cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a custom chain/operation returning custom error code 500

Paco_Alías
Confirmed Champ
Confirmed Champ

Hi all

I'm implementing a custom operation and using chainException framework to capture exception thrown in a chain, based on:

https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception

<extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
	point="chains">

	<chain id="**customExceptionReturn**">
		<operation id="**Custom.Exception.Create**" />
	</chain>

And it works ok. I can select the chain to be thrown when my custom chain fails and I can return a custom exception from this chain. But the webEngine returns a HTTP 200 code when I want it to return a 500.

This is my Exception public class CustomException extends RestOperationException {}

and how is it used in operation Custom.Exception.Create :

	@OperationMethod()
	public CustomException run() throws CustomException{
		CustomException retVal = null;
		Object excName = ctx.get("exceptionName");
		Object excObject = ctx.get("exceptionObject");
		_log.error("Registering new CustomException: " + excObject);
		retVal = (CustomException)excObject;
		retVal.setStatus(500);
	}

The exception is extracted correctly and I can set the correct message, but it throws a 200 code instead a 500 (as set in my code).

This is the result:

{
  "entity-type": "org.custom.nuxeo.operations.exception.CustomException",
  "value": {
    "className": "org.custom.nuxeo.operations.exception.CustomException",
    "status": 500,
    "rollback": true,
    "cause": null,
    "message": "OLEEEE is not a registered core type",
    "localizedMessage": "OLEEEE is not a registered core type",
 ...

And I want this this way:

{
  "entity-type": "exception",
  "code": "org.nuxeo.ecm.automation.TraceException",
  "status": 500,
  "stacktrace": "org.nuxeo.ecm.webengine.W

I've take a look at https://github.com/nuxeo/nuxeo/blob/6.0-HF27/nuxeo-features/rest-api/nuxeo-rest-api-server/src/main/...

But I could not get it working.

Any ideas please?

Thanks

1 REPLY 1

Paco_Alías
Confirmed Champ
Confirmed Champ

Finally I found a solution,

  1. Create your own Exception

public class MyException extends RestOperationException {

Be sure to extend RestOperationException

  1. In your operation, throw your exception
...
MyException exc = new MyException(e.getMessage(), e);
...

If you are using a chain instead an operation, take a look at that https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception

And, in your custom operation to catch the exception (chainExceptionA in the example) do this:

	@OperationMethod()
	public OperationException run() throws OperationException{
		MyException retVal = null;
		if (_log.isDebugEnabled()) {
		    _log.debug("Running operation: " + MyExceptionCreateOperation.ID);
		}
		Object excName = ctx.get("exceptionName");
		Object excObject = ctx.get("exceptionObject");
		_log.error("Registering new MyException: " + excObject, (Throwable)excObject);
		int returnCode = 500;
		if (excObject instanceof MyException) {
			retVal = (MyException)excObject;
			returnCode = 400;
		} else if (excObject instanceof Throwable) {
			Throwable t = (Throwable)excObject;
			retVal = new MyException(t.getMessage());
		} else {
			if (StringUtils.isNullOrEmpty(message)) {
				message = "Unexpected exception [" + excName + "]: " 
					+ excObject.getClass().getName();
			}
			retVal = new MyException(message);
		}
		((RestOperationContext)ctx).setHttpStatus(returnCode);
**		retVal.setStatus(returnCode);**
		return retVal;
	}
Getting started

Attention: Blogs & Forums are in READ-ONLY mode from Oct 9-19 as we transition to our new platform. Learn more.

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.