2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.impl.util;
10 import io.netty.channel.ChannelHandler;
11 import io.netty.channel.ChannelHandlerContext;
12 import java.util.HashMap;
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.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public final class DeserializerExceptionHandler implements ChannelHandler {
22 private static final Logger LOG = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
25 public void handlerAdded(final ChannelHandlerContext ctx) {
30 public void handlerRemoved(final ChannelHandlerContext ctx) {
35 public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
36 LOG.warn("An exception occurred during message handling", cause);
37 handleDeserializerException(ctx, cause);
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 DocumentedException.ErrorType.RPC, DocumentedException.ErrorTag.MALFORMED_MESSAGE,
45 ErrorSeverity.ERROR, info);
47 SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);