Reputation: 1563
I've searching around for an exception handling pattern for Netty but I'm not able to find much.
Some sort of exception handling guide would be great. I have exceptions thrown that are sent to exceptionCaught but I don't know what to do next.
Can someone provide a general purpose explanation of how to handle exceptions in Netty. What is the expected pattern for handling an exception thrown from a ChannelHandler?
Thanks, Matt
Upvotes: 9
Views: 12381
Reputation: 326
As Norman and Veebs have both mentioned, without understanding your precise requirements it's a little tricky to give a precise answer however.... I think the following provides a generic way to handle server errors that you were not expecting. It returns an HTTP 500 'Internal Server Error' to the client and then closes the channel. Obviously I'm making the assumption that your clients are requesting and receiving over HTTP which they may not be, in which case Veebs's solution is better.
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
public class ServerErrorHandler extends SimpleChannelHandler {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
HttpResponse err = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.INTERNAL_SERVER_ERROR);
e.getChannel().write(err).addListener(ChannelFutureListener.CLOSE);
}
}
Note if you use this solution then you will need to add an HttpResponseDecoder to your pipeline also.
Obviously if you have specific exceptions that you wish to catch and handle then you'd write some additional logic here to do that.
HTH!
Upvotes: 3
Reputation: 2388
Agree with Norman.
In general, I try to catch and handle all application exception and return proper messages containing the errors.
For example, in a HTTP server, I would return a 404 if a file was not found.
I also add the following function in my handler for any exceptions I did not catch - which in theory should only be network type errors. I tend to take a black and white approach to these exceptions and assume that I cannot recover. Hence, I close the channel. It will be up to the client to try again.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
try {
_logger.error(e.getCause(), "ERROR: Unhandled exception: " + e.getCause().getMessage()
+ ". Closing channel " + ctx.getChannel().getId());
e.getChannel().close();
} catch (Exception ex) {
_logger.debug(ex, "ERROR trying to close socket because we got an unhandled exception");
}
}
Hope this helps.
Upvotes: 2
Reputation: 23557
It really depends on your implementation and what type of Exception. Sometimes you may be able to recover, other times it may be the best to just close the Channel.
So I think its impossible to tell you how to handle it..
Upvotes: 2