You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Leen Toelen <to...@gmail.com> on 2010/03/02 16:25:01 UTC

Return exception message in HTTP body

Hi,

I am trying to run this route:

		<route id="RestToTopic">
			<from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
			<doTry>
				<to uri="xslt:XSLT.xml" />
				<to uri="validator:XSD.xsd" />
				<inOnly uri="activemq:topic:Topic" />
				<setBody><constant></constant></setBody> <!-- Return empty body when OK -->
				<doCatch>
		            	<exception>org.apache.camel.ValidationException</exception>
					<handled><constant>false</constant></handled>
					<!-- TODO: Return exception message on ValidationException -->
		            </doCatch>
			</doTry>
		</route>

but I would like to get the validation exception message as the result
body. Is this possible?

Regards,
Leen

Re: Return exception message in HTTP body

Posted by Willem Jiang <wi...@gmail.com>.
Hi

I just committed a quick fix for it.
http://svn.apache.org/viewvc?rev=918453&view=rev

Willem


Claus Ibsen wrote:
> Hi
> 
> Here is the JIRA ticket
> https://issues.apache.org/activemq/browse/CAMEL-2519
> 
> On Wed, Mar 3, 2010 at 9:25 AM, Claus Ibsen <cl...@gmail.com> wrote:
>> Hi
>>
>> The custom binding is a good solution IMHO since you got a fair amount
>> of logic to return different error codes.
>>
>> But you can also check out this user thread about custom error codes
>> http://old.nabble.com/Default-error-handler%2C-changing-HTTP-response-code-ts27721146.html
>>
>> And maybe we should support setting a custom
>> Exchange.HTTP_RESPONSE_CODE code to be used for exceptions as well.
>> Let me create a JIRA for that.
>>
>>
>> On Wed, Mar 3, 2010 at 9:08 AM, Leen Toelen <to...@gmail.com> wrote:
>>> Hi,
>>>
>>> I had to use a custom httpBinding, since the DefaultHttpBinding
>>> doWriteExceptionResponse always writes error code 500. I had to change
>>> this because I wanted to have distinct error code depending on what
>>> exactly went wrong. If the XSD validating fails, I return a 400 Bad
>>> Request, when the queue is full I return a 504 Gateway timeout. The
>>> client can than handle the response in different ways if he wants.
>>>
>>> Is it intentionally that 500 is always returned by default or is there
>>> a different way to specify the error code? I tried using the
>>> Exchange.HTTP_RESPONSE_CODE header but this does only work for
>>> non-exception cases.
>>>
>>>        @Override
>>>        public void doWriteExceptionResponse(Throwable exception,
>>>                        HttpServletResponse response) throws IOException {
>>>                int errorCode = 500;
>>>                if (exception instanceof ValidationException) {
>>>                        errorCode = HttpServletResponse.SC_BAD_REQUEST;
>>>                } else if (exception instanceof org.apache.camel.ExchangeTimedOutException) {
>>>                        errorCode = HttpServletResponse.SC_GATEWAY_TIMEOUT;
>>>                } else if (exception instanceof RollbackExchangeException) {
>>>                        errorCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
>>>                } else {
>>>                        errorCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
>>>                }
>>>                response.setStatus(errorCode); // 500 for internal server error
>>>                response.setContentType("text/plain");
>>>
>>>                // append the stacktrace as response
>>>                PrintWriter pw = response.getWriter();
>>>                exception.printStackTrace(pw);
>>>
>>>                pw.flush();
>>>        }
>>>
>>> Regards,
>>> Leen
>>>
>>> On Tue, Mar 2, 2010 at 4:46 PM, Claus Ibsen <cl...@gmail.com> wrote:
>>>> On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <to...@gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> I am trying to run this route:
>>>>>
>>>>>                <route id="RestToTopic">
>>>>>                        <from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
>>>>>                        <doTry>
>>>>>                                <to uri="xslt:XSLT.xml" />
>>>>>                                <to uri="validator:XSD.xsd" />
>>>>>                                <inOnly uri="activemq:topic:Topic" />
>>>>>                                <setBody><constant></constant></setBody> <!-- Return empty body when OK -->
>>>>>                                <doCatch>
>>>>>                                <exception>org.apache.camel.ValidationException</exception>
>>>>>                                        <handled><constant>false</constant></handled>
>>>>>                                        <!-- TODO: Return exception message on ValidationException -->
>>>>>                            </doCatch>
>>>>>                        </doTry>
>>>>>                </route>
>>>>>
>>>>> but I would like to get the validation exception message as the result
>>>>> body. Is this possible?
>>>>>
>>>> Yeah from the simple language etc.
>>>> http://camel.apache.org/simple.html
>>>>
>>>> <setBody><simple>${exception.message}</simple></setBody>
>>>>
>>>>> Regards,
>>>>> Leen
>>>>>
>>>>
>>>>
>>>> --
>>>> Claus Ibsen
>>>> Apache Camel Committer
>>>>
>>>> Author of Camel in Action: http://www.manning.com/ibsen/
>>>> Open Source Integration: http://fusesource.com
>>>> Blog: http://davsclaus.blogspot.com/
>>>> Twitter: http://twitter.com/davsclaus
>>>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
> 
> 
> 


Re: Return exception message in HTTP body

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Here is the JIRA ticket
https://issues.apache.org/activemq/browse/CAMEL-2519

On Wed, Mar 3, 2010 at 9:25 AM, Claus Ibsen <cl...@gmail.com> wrote:
> Hi
>
> The custom binding is a good solution IMHO since you got a fair amount
> of logic to return different error codes.
>
> But you can also check out this user thread about custom error codes
> http://old.nabble.com/Default-error-handler%2C-changing-HTTP-response-code-ts27721146.html
>
> And maybe we should support setting a custom
> Exchange.HTTP_RESPONSE_CODE code to be used for exceptions as well.
> Let me create a JIRA for that.
>
>
> On Wed, Mar 3, 2010 at 9:08 AM, Leen Toelen <to...@gmail.com> wrote:
>> Hi,
>>
>> I had to use a custom httpBinding, since the DefaultHttpBinding
>> doWriteExceptionResponse always writes error code 500. I had to change
>> this because I wanted to have distinct error code depending on what
>> exactly went wrong. If the XSD validating fails, I return a 400 Bad
>> Request, when the queue is full I return a 504 Gateway timeout. The
>> client can than handle the response in different ways if he wants.
>>
>> Is it intentionally that 500 is always returned by default or is there
>> a different way to specify the error code? I tried using the
>> Exchange.HTTP_RESPONSE_CODE header but this does only work for
>> non-exception cases.
>>
>>        @Override
>>        public void doWriteExceptionResponse(Throwable exception,
>>                        HttpServletResponse response) throws IOException {
>>                int errorCode = 500;
>>                if (exception instanceof ValidationException) {
>>                        errorCode = HttpServletResponse.SC_BAD_REQUEST;
>>                } else if (exception instanceof org.apache.camel.ExchangeTimedOutException) {
>>                        errorCode = HttpServletResponse.SC_GATEWAY_TIMEOUT;
>>                } else if (exception instanceof RollbackExchangeException) {
>>                        errorCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
>>                } else {
>>                        errorCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
>>                }
>>                response.setStatus(errorCode); // 500 for internal server error
>>                response.setContentType("text/plain");
>>
>>                // append the stacktrace as response
>>                PrintWriter pw = response.getWriter();
>>                exception.printStackTrace(pw);
>>
>>                pw.flush();
>>        }
>>
>> Regards,
>> Leen
>>
>> On Tue, Mar 2, 2010 at 4:46 PM, Claus Ibsen <cl...@gmail.com> wrote:
>>> On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <to...@gmail.com> wrote:
>>>> Hi,
>>>>
>>>> I am trying to run this route:
>>>>
>>>>                <route id="RestToTopic">
>>>>                        <from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
>>>>                        <doTry>
>>>>                                <to uri="xslt:XSLT.xml" />
>>>>                                <to uri="validator:XSD.xsd" />
>>>>                                <inOnly uri="activemq:topic:Topic" />
>>>>                                <setBody><constant></constant></setBody> <!-- Return empty body when OK -->
>>>>                                <doCatch>
>>>>                                <exception>org.apache.camel.ValidationException</exception>
>>>>                                        <handled><constant>false</constant></handled>
>>>>                                        <!-- TODO: Return exception message on ValidationException -->
>>>>                            </doCatch>
>>>>                        </doTry>
>>>>                </route>
>>>>
>>>> but I would like to get the validation exception message as the result
>>>> body. Is this possible?
>>>>
>>>
>>> Yeah from the simple language etc.
>>> http://camel.apache.org/simple.html
>>>
>>> <setBody><simple>${exception.message}</simple></setBody>
>>>
>>>> Regards,
>>>> Leen
>>>>
>>>
>>>
>>>
>>> --
>>> Claus Ibsen
>>> Apache Camel Committer
>>>
>>> Author of Camel in Action: http://www.manning.com/ibsen/
>>> Open Source Integration: http://fusesource.com
>>> Blog: http://davsclaus.blogspot.com/
>>> Twitter: http://twitter.com/davsclaus
>>>
>>
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: Return exception message in HTTP body

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

The custom binding is a good solution IMHO since you got a fair amount
of logic to return different error codes.

But you can also check out this user thread about custom error codes
http://old.nabble.com/Default-error-handler%2C-changing-HTTP-response-code-ts27721146.html

And maybe we should support setting a custom
Exchange.HTTP_RESPONSE_CODE code to be used for exceptions as well.
Let me create a JIRA for that.


On Wed, Mar 3, 2010 at 9:08 AM, Leen Toelen <to...@gmail.com> wrote:
> Hi,
>
> I had to use a custom httpBinding, since the DefaultHttpBinding
> doWriteExceptionResponse always writes error code 500. I had to change
> this because I wanted to have distinct error code depending on what
> exactly went wrong. If the XSD validating fails, I return a 400 Bad
> Request, when the queue is full I return a 504 Gateway timeout. The
> client can than handle the response in different ways if he wants.
>
> Is it intentionally that 500 is always returned by default or is there
> a different way to specify the error code? I tried using the
> Exchange.HTTP_RESPONSE_CODE header but this does only work for
> non-exception cases.
>
>        @Override
>        public void doWriteExceptionResponse(Throwable exception,
>                        HttpServletResponse response) throws IOException {
>                int errorCode = 500;
>                if (exception instanceof ValidationException) {
>                        errorCode = HttpServletResponse.SC_BAD_REQUEST;
>                } else if (exception instanceof org.apache.camel.ExchangeTimedOutException) {
>                        errorCode = HttpServletResponse.SC_GATEWAY_TIMEOUT;
>                } else if (exception instanceof RollbackExchangeException) {
>                        errorCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
>                } else {
>                        errorCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
>                }
>                response.setStatus(errorCode); // 500 for internal server error
>                response.setContentType("text/plain");
>
>                // append the stacktrace as response
>                PrintWriter pw = response.getWriter();
>                exception.printStackTrace(pw);
>
>                pw.flush();
>        }
>
> Regards,
> Leen
>
> On Tue, Mar 2, 2010 at 4:46 PM, Claus Ibsen <cl...@gmail.com> wrote:
>> On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <to...@gmail.com> wrote:
>>> Hi,
>>>
>>> I am trying to run this route:
>>>
>>>                <route id="RestToTopic">
>>>                        <from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
>>>                        <doTry>
>>>                                <to uri="xslt:XSLT.xml" />
>>>                                <to uri="validator:XSD.xsd" />
>>>                                <inOnly uri="activemq:topic:Topic" />
>>>                                <setBody><constant></constant></setBody> <!-- Return empty body when OK -->
>>>                                <doCatch>
>>>                                <exception>org.apache.camel.ValidationException</exception>
>>>                                        <handled><constant>false</constant></handled>
>>>                                        <!-- TODO: Return exception message on ValidationException -->
>>>                            </doCatch>
>>>                        </doTry>
>>>                </route>
>>>
>>> but I would like to get the validation exception message as the result
>>> body. Is this possible?
>>>
>>
>> Yeah from the simple language etc.
>> http://camel.apache.org/simple.html
>>
>> <setBody><simple>${exception.message}</simple></setBody>
>>
>>> Regards,
>>> Leen
>>>
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: Return exception message in HTTP body

Posted by Leen Toelen <to...@gmail.com>.
Hi,

I had to use a custom httpBinding, since the DefaultHttpBinding
doWriteExceptionResponse always writes error code 500. I had to change
this because I wanted to have distinct error code depending on what
exactly went wrong. If the XSD validating fails, I return a 400 Bad
Request, when the queue is full I return a 504 Gateway timeout. The
client can than handle the response in different ways if he wants.

Is it intentionally that 500 is always returned by default or is there
a different way to specify the error code? I tried using the
Exchange.HTTP_RESPONSE_CODE header but this does only work for
non-exception cases.

	@Override
	public void doWriteExceptionResponse(Throwable exception,
			HttpServletResponse response) throws IOException {
		int errorCode = 500;
		if (exception instanceof ValidationException) {
			errorCode = HttpServletResponse.SC_BAD_REQUEST;
		} else if (exception instanceof org.apache.camel.ExchangeTimedOutException) {
			errorCode = HttpServletResponse.SC_GATEWAY_TIMEOUT;
		} else if (exception instanceof RollbackExchangeException) {
			errorCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
		} else {
			errorCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
		}
		response.setStatus(errorCode); // 500 for internal server error
		response.setContentType("text/plain");

		// append the stacktrace as response
		PrintWriter pw = response.getWriter();
		exception.printStackTrace(pw);

		pw.flush();
	}

Regards,
Leen

On Tue, Mar 2, 2010 at 4:46 PM, Claus Ibsen <cl...@gmail.com> wrote:
> On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <to...@gmail.com> wrote:
>> Hi,
>>
>> I am trying to run this route:
>>
>>                <route id="RestToTopic">
>>                        <from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
>>                        <doTry>
>>                                <to uri="xslt:XSLT.xml" />
>>                                <to uri="validator:XSD.xsd" />
>>                                <inOnly uri="activemq:topic:Topic" />
>>                                <setBody><constant></constant></setBody> <!-- Return empty body when OK -->
>>                                <doCatch>
>>                                <exception>org.apache.camel.ValidationException</exception>
>>                                        <handled><constant>false</constant></handled>
>>                                        <!-- TODO: Return exception message on ValidationException -->
>>                            </doCatch>
>>                        </doTry>
>>                </route>
>>
>> but I would like to get the validation exception message as the result
>> body. Is this possible?
>>
>
> Yeah from the simple language etc.
> http://camel.apache.org/simple.html
>
> <setBody><simple>${exception.message}</simple></setBody>
>
>> Regards,
>> Leen
>>
>
>
>
> --
> Claus Ibsen
> Apache Camel Committer
>
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
>

Re: Return exception message in HTTP body

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <to...@gmail.com> wrote:
> Hi,
>
> I am trying to run this route:
>
>                <route id="RestToTopic">
>                        <from uri="jetty:http://0.0.0.0:8162/meucci/assembly" />
>                        <doTry>
>                                <to uri="xslt:XSLT.xml" />
>                                <to uri="validator:XSD.xsd" />
>                                <inOnly uri="activemq:topic:Topic" />
>                                <setBody><constant></constant></setBody> <!-- Return empty body when OK -->
>                                <doCatch>
>                                <exception>org.apache.camel.ValidationException</exception>
>                                        <handled><constant>false</constant></handled>
>                                        <!-- TODO: Return exception message on ValidationException -->
>                            </doCatch>
>                        </doTry>
>                </route>
>
> but I would like to get the validation exception message as the result
> body. Is this possible?
>

Yeah from the simple language etc.
http://camel.apache.org/simple.html

<setBody><simple>${exception.message}</simple></setBody>

> Regards,
> Leen
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus