Rename netconf-impl to netconf-server
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / util / DeserializerExceptionHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.server.util;
9
10 import io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.ChannelInboundHandlerAdapter;
12 import java.util.Map;
13 import org.opendaylight.netconf.api.DocumentedException;
14 import org.opendaylight.netconf.server.SendErrorExceptionUtil;
15 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
16 import org.opendaylight.yangtools.yang.common.ErrorTag;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class DeserializerExceptionHandler extends ChannelInboundHandlerAdapter {
22     private static final Logger LOG = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
23
24     @Override
25     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
26         LOG.warn("An exception occurred during message handling", cause);
27         handleDeserializerException(ctx, cause);
28     }
29
30     private static void handleDeserializerException(final ChannelHandlerContext ctx, final Throwable cause) {
31         final String message = cause.getMessage();
32         SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), new DocumentedException(message,
33             ErrorType.RPC, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR,
34             message != null ? Map.of("cause", message) : Map.of()));
35     }
36 }