@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 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();
 }