### Question:
DeFramer extends ByteToMessageDecoder { @Override public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable throwable) throws Exception { final Throwable cause = throwable instanceof DecoderException && throwable.getCause() != null ? throwable.getCause() : throwable; if (cause instanceof FramingException || cause instanceof RLPException || cause instanceof IllegalArgumentException) { LOG.debug("Invalid incoming message", throwable); if (connectFuture.isDone() && !connectFuture.isCompletedExceptionally()) { connectFuture.get().disconnect(DisconnectMessage.DisconnectReason.BREACH_OF_PROTOCOL); return; } } else if (cause instanceof IOException) { LOG.debug("IO error while processing incoming message", throwable); } else { LOG.error("Exception while processing incoming message", throwable); } if (connectFuture.isDone() && !connectFuture.isCompletedExceptionally()) { connectFuture .get() .terminateConnection(DisconnectMessage.DisconnectReason.TCP_SUBSYSTEM_ERROR, true); } else { connectFuture.completeExceptionally(throwable); ctx.close(); } } DeFramer(
      final Framer framer,
      final List<SubProtocol> subProtocols,
      final LocalNode localNode,
      final Optional<Peer> expectedPeer,
      final PeerConnectionEventDispatcher connectionEventDispatcher,
      final CompletableFuture<PeerConnection> connectFuture,
      final MetricsSystem metricsSystem); @Override void exceptionCaught(final ChannelHandlerContext ctx, final Throwable throwable);  }### Answer:
@Test public void exceptionCaught_shouldDisconnectForBreachOfProtocolWhenFramingExceptionThrown() throws Exception { connectFuture.complete(peerConnection); deFramer.exceptionCaught(ctx, new DecoderException(new FramingException("Test"))); verify(peerConnection).disconnect(DisconnectReason.BREACH_OF_PROTOCOL); }
@Test public void exceptionCaught_shouldDisconnectForBreachOfProtocolWhenRlpExceptionThrown() throws Exception { connectFuture.complete(peerConnection); deFramer.exceptionCaught(ctx, new DecoderException(new RLPException("Test"))); verify(peerConnection).disconnect(DisconnectReason.BREACH_OF_PROTOCOL); }
@Test public void exceptionCaught_shouldHandleFramingExceptionWhenFutureCompletedExceptionally() throws Exception { connectFuture.completeExceptionally(new Exception()); deFramer.exceptionCaught(ctx, new DecoderException(new FramingException("Test"))); verify(ctx).close(); }
@Test public void exceptionCaught_shouldHandleGenericExceptionWhenFutureCompletedExceptionally() throws Exception { connectFuture.completeExceptionally(new Exception()); deFramer.exceptionCaught(ctx, new DecoderException(new RuntimeException("Test"))); verify(ctx).close(); }