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

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 point=“chainException”

  target="org.nuxeo.ecm.core.operation.OperationServiceComponent">
  <catchChain id="**customExceptionCatch**" onChainId="Document.Create">
      <run chainId="**customExceptionReturn**" priority="0" rollBack="true" />
  </catchChain>

<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/java/org/nuxeo/ecm/restapi/server/jaxrs/adapters/OperationAdapter.java#L94

But I could not get it working.

Any ideas please?

Thanks

0 votes

1 answers

3263 views

ANSWER



Finally I found a solution,

1) Create your own Exception

public class MyException extends RestOperationException {

Be sure to extend RestOperationException

2) 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;
    }
1 votes