How to handle exception in decoders/handlers?
"이희승 (Trustin Lee)"
trustin at gmail.com
Fri Jun 19 14:43:06 EDT 2009
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 lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/netty-users
>>
>>
>
--
— Trustin Lee, http://gleamynode.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
Url : http://lists.jboss.org/pipermail/netty-users/attachments/20090620/c4cf9466/attachment.bin
More information about the netty-users
mailing list