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:

<extension point=“chainException”

  <catchChain id="**customExceptionCatch**" onChainId="Document.Create">
      <run chainId="**customExceptionReturn**" priority="0" rollBack="true" />

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

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

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 :

    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;

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

But I could not get it working.

Any ideas please?


0 votes

1 answers



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

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

    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);
**      retVal.setStatus(returnCode);**
        return retVal;
1 votes