<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to make a custom chain/operation returning custom error code 500 in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-make-a-custom-chain-operation-returning-custom-error-code/m-p/314022#M1023</link>
    <description>&lt;P&gt;Finally I found a solution,&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create your own Exception&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;CODE&gt;public class MyException extends RestOperationException {&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Be sure to extend &lt;CODE&gt;RestOperationException&lt;/CODE&gt;&lt;/P&gt;
&lt;OL start="2"&gt;
&lt;LI&gt;In your operation, throw your exception&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE&gt;...
MyException exc = new MyException(e.getMessage(), e);
...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are using a chain instead an operation, take a look at that &lt;A href="https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception" target="test_blank"&gt;https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;And, in your custom operation to catch the exception (chainExceptionA in the example) do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;	@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;
	}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Jun 2016 17:02:16 GMT</pubDate>
    <dc:creator>Paco_Alías</dc:creator>
    <dc:date>2016-06-21T17:02:16Z</dc:date>
    <item>
      <title>How to make a custom chain/operation returning custom error code 500</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-make-a-custom-chain-operation-returning-custom-error-code/m-p/314021#M1022</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;I'm implementing a custom operation and using chainException framework to capture exception thrown in a chain, based on:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception" target="test_blank"&gt;https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception&lt;/A&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;EXTENSION point="chainException" target="org.nuxeo.ecm.core.operation.OperationServiceComponent"&gt;
&lt;CATCHCHAIN id="**customExceptionCatch**" onchainid="Document.Create"&gt;
&lt;RUN chainid="**customExceptionReturn**" priority="0" rollback="true"&gt;&lt;/RUN&gt;
&lt;/CATCHCHAIN&gt;
&lt;/EXTENSION&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE&gt;&amp;lt;extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
	point="chains"&amp;gt;

	&amp;lt;chain id="**customExceptionReturn**"&amp;gt;
		&amp;lt;operation id="**Custom.Exception.Create**" /&amp;gt;
	&amp;lt;/chain&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;This is my Exception
&lt;CODE&gt;public class CustomException extends RestOperationException {&lt;/CODE&gt;}&lt;/P&gt;
&lt;P&gt;and how is it used in operation Custom.Exception.Create :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;	@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);
	}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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).&lt;/P&gt;
&lt;P&gt;This is the result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;{
  "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",
 ...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I want this this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;{
  "entity-type": "exception",
  "code": "org.nuxeo.ecm.automation.TraceException",
  "status": 500,
  "stacktrace": "org.nuxeo.ecm.webengine.W
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've take a look at
&lt;A href="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" target="test_blank"&gt;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&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;But I could not get it working.&lt;/P&gt;
&lt;P&gt;Any ideas please?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2016 18:06:57 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-make-a-custom-chain-operation-returning-custom-error-code/m-p/314021#M1022</guid>
      <dc:creator>Paco_Alías</dc:creator>
      <dc:date>2016-06-15T18:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a custom chain/operation returning custom error code 500</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-make-a-custom-chain-operation-returning-custom-error-code/m-p/314022#M1023</link>
      <description>&lt;P&gt;Finally I found a solution,&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create your own Exception&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;CODE&gt;public class MyException extends RestOperationException {&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Be sure to extend &lt;CODE&gt;RestOperationException&lt;/CODE&gt;&lt;/P&gt;
&lt;OL start="2"&gt;
&lt;LI&gt;In your operation, throw your exception&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE&gt;...
MyException exc = new MyException(e.getMessage(), e);
...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are using a chain instead an operation, take a look at that &lt;A href="https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception" target="test_blank"&gt;https://doc.nuxeo.com/display/NXDOC/Automation+Chain+Exception&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;And, in your custom operation to catch the exception (chainExceptionA in the example) do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;	@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;
	}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 17:02:16 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-make-a-custom-chain-operation-returning-custom-error-code/m-p/314022#M1023</guid>
      <dc:creator>Paco_Alías</dc:creator>
      <dc:date>2016-06-21T17:02:16Z</dc:date>
    </item>
  </channel>
</rss>

