How to handle exception in decoders/handlers?

infectedrhythms voodoo at videotron.ca
Fri Jun 19 14:44:53 EDT 2009


Is there a simple sample to follow?

Trustin Lee (via Nabble) wrote:
> You need an encoder.
>
> On 2009-06-20 오전 3:21, infectedrhythms wrote:
>
> >
> > Ok so now the exception is the last handler.
> >
> > But how do I write to the socket as
> >
> > @Override
> > public void exceptionCaught(ChannelHandlerContext ctx, 
> ExceptionEvent e) {
> >
> > e.getCause().printStackTrace();
> >
> > e.getChannel().write("Test");
> >
> >
> > //ChannelFuture cf = ch.write("Test");
> >
> > //cf.addListener(ChannelFutureListener.CLOSE);
> > //ch.close();
> > }
> >
> > throws...
> >
> >
> > java.lang.ClassCastException: java.lang.String cannot be cast to
> > org.jboss.netty.buffer.ChannelBuffer
> > at
> > 
> org.jboss.netty.channel.socket.nio.NioWorker.writeUnfair(NioWorker.java:438) 
>
> > at 
> org.jboss.netty.channel.socket.nio.NioWorker.write(NioWorker.java:411)
> > at
> > 
> org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:138) 
>
> > at
> > 
> org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:78) 
>
> > at org.jboss.netty.channel.Channels.write(Channels.java:626)
> > at org.jboss.netty.channel.Channels.write(Channels.java:592)
> > at 
> org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:203)
> > at
> > 
> com.rbs.threedsecure.mpi.services.broker.ThreeDHandler.exceptionCaught(ThreeDHandler.java:51) 
>
> > at
> > 
> com.rbs.threedsecure.mpi.services.broker.ThreeDHandler.handleUpstream(ThreeDHandler.java:32) 
>
> > at
> > 
> org.jboss.netty.handler.codec.frame.FrameDecoder.exceptionCaught(FrameDecoder.java:194) 
>
> > at 
> org.jboss.netty.channel.Channels.fireExceptionCaught(Channels.java:440)
> > at
> > 
> org.jboss.netty.channel.AbstractChannelSink.exceptionCaught(AbstractChannelSink.java:59) 
>
> > at 
> org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:293)
> > at 
> org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:280)
> > at
> > 
> org.jboss.netty.channel.socket.nio.NioWorker.readIntoHeapBuffer(NioWorker.java:300) 
>
> > at
> > 
> org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:254) 
>
> > at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:163)
> > at
> > 
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
>
> > at
> > 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
>
> > at java.lang.Thread.run(Thread.java:619)
> >
> >
> >
> > Trustin Lee wrote:
> >>
> >> Hi,
> >>
> >> It seems like one of the handlers in your pipeline closes the 
> connection
> >> on an exceptionCaught event (perhaps the last handler.)  I would catch
> >> the exception in the last handler and send the error message there
> >> rather than sending it in the decoder, because it's often a good 
> idea to
> >> separate protocol state from codec.
> >>
> >> HTH,
> >> Trustin
> >>
> >> On 2009-06-20 오전 2:42, infectedrhythms wrote:
> >>>
> >>> Hi, I have implemented a simple decoder and it works fine.
> >>>
> >>> Now if there is an error I want to throw an exception catch and reply
> >>> back
> >>> to the client, kind of like an HTTP 500 error.
> >>>
> >>> So if the decoder fails it should write back to the client the 
> error that
> >>> occurred.
> >>>
> >>> @Override
> >>> protected Object decode(ChannelHandlerContext ctx, Channel channel,
> >>> ChannelBuffer buf) throws Exception {
> >>>
> >>> ... do stuff here...
> >>>
> >>> throw new Exception("Errrorrrr!");
> >>> }
> >>>
> >>> @Override
> >>> public void exceptionCaught(ChannelHandlerContext ctx, 
> ExceptionEvent e)
> >>> throws Exception {
> >>>
> >>> super.exceptionCaught(ctx, e);
> >>>
> >>> e.getChannel().write("Damn it Jim!"); <-- Throws a
> >>> ClosedChannelException
> >>>
> >>> }
> >>
> >>
> >> --
> >> — Trustin Lee, http://gleamynode.net/
> >>
> >>
> >>  
> >> _______________________________________________
> >> netty-users mailing list
> >> netty-users at ... 
> <http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3121435&i=0>
> >> https://lists.jboss.org/mailman/listinfo/netty-users
> >>
> >>
> >
>
> -- 
> — Trustin Lee, http://gleamynode.net/
>
>
>
> _______________________________________________
> netty-users mailing list
> netty-users at ... 
> <http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3121435&i=1>
> https://lists.jboss.org/mailman/listinfo/netty-users
>
> *signature.asc* (266 bytes) Download Attachment 
> <http://n2.nabble.com/attachment/3121435/0/signature.asc>
> Trustin Lee, Principal Software Engineer, JBoss, a division of Red Hat
> -- 
> what we call human nature is actually human habit
> -- 
> http://gleamynode.net/
>
>
> ------------------------------------------------------------------------
> This email is a reply to your post @ 
> http://n2.nabble.com/How-to-handle-exception-in-decoders-handlers--tp3121070p3121435.html
> You can reply by email or by visting the link above.
>



-- 
View this message in context: http://n2.nabble.com/How-to-handle-exception-in-decoders-handlers--tp3121070p3121442.html
Sent from the Netty User Group mailing list archive at Nabble.com.





More information about the netty-users mailing list