@Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
   if (e.getCause() instanceof JsonParseException) {
     BaseTransport.respond(
         e.getChannel(), HttpResponseStatus.INTERNAL_SERVER_ERROR, "Broken JSON encoding.");
   } else if (e.getCause() instanceof SessionHandler.NotFoundException) {
     BaseTransport.respond(
         e.getChannel(),
         HttpResponseStatus.NOT_FOUND,
         "Session not found. Cannot send data to non-existing session.");
   } else {
     super.exceptionCaught(ctx, e);
   }
 }
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
   Channel ch = e.getChannel();
   Throwable cause = e.getCause();
   if (cause instanceof TooLongFrameException) {
     sendError(ctx, HttpResponseStatus.BAD_REQUEST);
     return;
   }
   if (cause != null) {
     if (cause.getClass().equals(IOException.class)) {
       LOGGER.debug("Connection error: " + cause);
       StartStopListenerDelegate startStopListenerDelegate =
           (StartStopListenerDelegate) ctx.getAttachment();
       if (startStopListenerDelegate != null) {
         LOGGER.debug("Premature end, stopping...");
         startStopListenerDelegate.stop();
       }
     } else if (!cause.getClass().equals(ClosedChannelException.class)) {
       LOGGER.debug("Caught exception: " + cause);
     }
   }
   if (ch.isConnected()) {
     sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
   e.getChannel().close();
 }
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    Channel channel = ctx.getChannel();
    if (channel.getAttachment() != null
        && (Error.class.isAssignableFrom(channel.getAttachment().getClass())
            || WRITING.equals(channel.getAttachment().toString()))) {
      return;
    }

    if (!isReceived(ctx)) {
      return;
    }

    try {
      log.error(e.getCause().getMessage(), e.getCause());
      channel.setAttachment(new Error());
      DefaultHttpResponse response =
          new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
      response.setContent(
          ChannelBuffers.copiedBuffer(
              response.getStatus().toString() + ":" + e.getCause().getMessage(),
              CharsetUtil.UTF_8));
      response.headers().set(Names.CONTENT_LENGTH, response.getContent().readableBytes());
      response.headers().set(Names.SERVER, "NAVI/1.1.4(UNIX)");
      response.headers().set(Names.CONNECTION, "close");
      ChannelFuture f = channel.write(response);
      f.addListener(ChannelFutureListener.CLOSE);
      f.addListener(
          new ChannelFutureListener() {

            public void operationComplete(ChannelFuture f) throws Exception {
              if (!f.isSuccess()) {
                log.error(f.getCause().getMessage(), f.getCause());
              }
            }
          });
    } catch (Exception ex) {
      log.error(e.getCause().getMessage(), e.getCause());
      e.getFuture().addListener(ChannelFutureListener.CLOSE);
    }
  }
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    Channel ch = e.getChannel();
    Throwable cause = e.getCause();
    if (cause instanceof TooLongFrameException) {
      sendError(ctx, BAD_REQUEST);
      return;
    }

    cause.printStackTrace();
    if (ch.isConnected()) {
      sendError(ctx, INTERNAL_SERVER_ERROR);
    }
  }
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
   Channel ch = e.getChannel();
   Throwable cause = e.getCause();
   if (cause instanceof TooLongFrameException) {
     sendError(ctx, HttpResponseStatus.BAD_REQUEST);
     return;
   }
   if (cause != null
       && !cause.getClass().equals(ClosedChannelException.class)
       && !cause.getClass().equals(IOException.class)) {
     LOGGER.debug("Caught exception", cause);
   }
   if (ch.isConnected()) {
     sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
   }
   e.getChannel().close();
 }
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
   e.getCause().printStackTrace();
 }