Remove DocumentedException.ErrorTag
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / 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.impl.util;
9
10 import io.netty.channel.ChannelHandler;
11 import io.netty.channel.ChannelHandlerContext;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.netconf.api.DocumentedException;
15 import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
16 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class DeserializerExceptionHandler implements ChannelHandler {
22     private static final Logger LOG = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
23
24     @Override
25     public void handlerAdded(final ChannelHandlerContext ctx) {
26         // NOOP
27     }
28
29     @Override
30     public void handlerRemoved(final ChannelHandlerContext ctx) {
31         // NOOP
32     }
33
34     @Override
35     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
36         LOG.warn("An exception occurred during message handling", cause);
37         handleDeserializerException(ctx, cause);
38     }
39
40     private static void handleDeserializerException(final ChannelHandlerContext ctx, final Throwable cause) {
41         final Map<String, String> info = new HashMap<>();
42         info.put("cause", cause.getMessage());
43         final DocumentedException ex = new DocumentedException(cause.getMessage(),
44                 ErrorType.RPC, DocumentedException.MALFORMED_MESSAGE, ErrorSeverity.ERROR, info);
45
46         SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);
47     }
48 }