protected void writeFailureResponse(Throwable exception, ChunkingChannelBuffer buffer) { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(exception); out.close(); buffer.writeBytes(bytes.toByteArray()); buffer.done(); } catch (IOException e) { msgLog.logMessage("Couldn't send cause of error to client", exception); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception { try { ChannelBuffer message = (ChannelBuffer) event.getMessage(); handleRequest(message, event.getChannel()); } catch (Throwable e) { msgLog.error("Error handling request", e); // Attempt to reply to the client ChunkingChannelBuffer buffer = newChunkingBuffer(event.getChannel()); buffer.clear(/* failure = */ true); writeFailureResponse(e, buffer); ctx.getChannel().close(); tryToFinishOffChannel(ctx.getChannel()); throw Exceptions.launderedException(e); } }