FinalUpstreamHandler extends SimpleChannelHandler { @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { Object attachment = e.getChannel().getAttachment(); Throwable channelException = e.getCause(); if (channelException instanceof ConnectException) { LOGGER.warn("{}: closing channel to {}: {}", self, attachment, channelException.getMessage()); } else { LOGGER.warn("{}: caught exception - closing channel to {}", self, attachment, channelException); } e.getChannel().close(); } FinalUpstreamHandler(String self); @Override void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e); @Override void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e); @Override void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e);  }
@Test public void shouldCloseChannelAndNotForwardEventWhenReceiveExceptionCaughtEvent() throws Exception { ExceptionEvent exceptionEvent = new DefaultExceptionEvent(channel, new Exception("test exception")); handler.exceptionCaught(ctx, exceptionEvent); verifyNoMoreInteractions(ctx); verify(channel).close(); }