Improve DeserializerExceptionHandler
[netconf.git] / 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()));
     }
 }