Improve DeserializerExceptionHandler 03/97303/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 23 Aug 2021 23:28:01 +0000 (01:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 24 Aug 2021 20:14:40 +0000 (22:14 +0200)
We are getting a deprecation warning, as there exceptionCaught() really
makes sense only on inbound side. This happens to be what we actually
are doing with DeserializerExceptionHandler anyway, so just pick up
ChannelInboundHandlerAdapter and use that as the superclass.

Change-Id: Ic661e4e85f4442a395c820463abf00e71c52543e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/util/DeserializerExceptionHandler.java

index 55631ef0eb6dfba066c15f18b8f49f417e2bbcb6..bd2561d3edb0c1b80b132fc2bea3e525c091455c 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.netconf.impl.util;
 
-import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
-import java.util.HashMap;
+import io.netty.channel.ChannelInboundHandlerAdapter;
 import java.util.Map;
 import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
@@ -18,19 +17,9 @@ import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class DeserializerExceptionHandler implements ChannelHandler {
+public final class DeserializerExceptionHandler extends ChannelInboundHandlerAdapter {
     private static final Logger LOG = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
 
-    @Override
-    public void handlerAdded(final ChannelHandlerContext ctx) {
-        // NOOP
-    }
-
-    @Override
-    public void handlerRemoved(final ChannelHandlerContext ctx) {
-        // NOOP
-    }
-
     @Override
     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
         LOG.warn("An exception occurred during message handling", cause);
@@ -38,11 +27,9 @@ public final class DeserializerExceptionHandler implements ChannelHandler {
     }
 
     private static void handleDeserializerException(final ChannelHandlerContext ctx, final Throwable cause) {
-        final Map<String, String> info = new HashMap<>();
-        info.put("cause", cause.getMessage());
-        final DocumentedException ex = new DocumentedException(cause.getMessage(),
-                ErrorType.RPC, DocumentedException.MALFORMED_MESSAGE, ErrorSeverity.ERROR, info);
-
-        SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);
+        final String message = cause.getMessage();
+        SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), new DocumentedException(message,
+            ErrorType.RPC, DocumentedException.MALFORMED_MESSAGE, ErrorSeverity.ERROR,
+            message != null ? Map.of("cause", message) : Map.of()));
     }
 }