Automation REST Response: how to return a custom message?

Following Automation REST Response documentation, we are able now to create a custom exception and return an appropiate http error code from our operations, as shown in the example below:

{
    "entity-type": "exception",
    "code": "org.nuxeo.ecm.automation.TraceException",
    "status": 600,
    "message": "Failed to invoke operation: Test.UpdateDocMetadata"
}

In this example 600 is our custom error code.

Now, we would like to be able to return a custom message in the exception. For example, instead of having Failed to invoke operation: Test.UpdateDocMetadata operation”, we would like to have a custom message. Is this posible?

We are using Nuxeo 7.2.

Thanks in advance and best regards,

1 votes

1 answers

1240 views

ANSWER



Hi!

I can think about several possibilities, depending on what you have done. If I only look at the example you gave, you can:

  • set your custom message in the ExceptionTest class;
  • Create a complete custom Exception where you set up your custom message and you don't necessarily have to extend RestOperationException (but you may have to extend class org.nuxeo.ecm.automation.OperationException)
  • In the HttpStatusOperationTest class (with ExceptionTest), set your custom message when constructing the ExceptionTest or with (ExceptionTest.)setMessage before throwing the Exception
  • In the HttpStatusOperationTest class (when success in Automation Operation), root is a DocumentModel, which has metadata. From your very example, it has a metadata named “message”, so you should be able to update it with setProperty or setPropertyValue before returning root.

I hope this helps.

0 votes