camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leen Toelen <toe...@gmail.com>
Subject Re: Return exception message in HTTP body
Date Wed, 03 Mar 2010 08:08:48 GMT
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 <claus.ibsen@gmail.com> wrote:
> On Tue, Mar 2, 2010 at 4:25 PM, Leen Toelen <toelen@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
>

Mime
View raw message