Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty exception handling - Handler throws Exception, then what?

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

like image 221
Matt Friedman Avatar asked Mar 24 '12 22:03

Matt Friedman


People also ask

What does an exception handler do?

An exception handler is code that stipulates what a program will do when an anomalous event disrupts the normal flow of that program's instructions. An exception, in a computer context, is an unplanned event that occurs while a program is executing and disrupts the flow of its instructions.

What are exceptions and exception handlers explain in detail with suitable examples?

Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program's instructions.


1 Answers

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!

like image 140
James K Avatar answered Sep 28 '22 16:09

James K