2 * Copyright (c) 2014 Brocade Communications 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.restconf.common.errors;
10 import static java.util.Objects.requireNonNull;
12 import java.io.Serializable;
13 import org.opendaylight.yangtools.yang.common.ErrorTag;
14 import org.opendaylight.yangtools.yang.common.ErrorType;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 * Encapsulates a restconf error as defined in the ietf restconf draft.
23 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
26 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
28 public class RestconfError implements Serializable {
29 private static final long serialVersionUID = 1L;
31 private final ErrorType errorType;
32 private final ErrorTag errorTag;
33 private final String errorInfo;
34 private final String errorAppTag;
35 private final String errorMessage;
36 private final YangInstanceIdentifier errorPath;
39 * Constructs a RestConfError.
42 * The enumerated type indicating the layer where the error occurred.
44 * The enumerated tag representing a more specific error cause.
46 * A string which provides a plain text string describing the error.
48 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
49 this(errorType, errorTag, errorMessage, null, null, null);
53 * Constructs a RestConfError object.
56 * The enumerated type indicating the layer where the error occurred.
58 * The enumerated tag representing a more specific error cause.
60 * A string which provides a plain text string describing the error.
62 * A string which represents an application-specific error tag that further specifies the error cause.
64 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
65 final String errorAppTag) {
66 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
70 * Constructs a RestConfError object.
73 * The enumerated type indicating the layer where the error occurred.
75 * The enumerated tag representing a more specific error cause.
77 * A string which provides a plain text string describing the error.
79 * An instance identifier which contains error path
81 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
82 final YangInstanceIdentifier errorPath) {
83 this(errorType, errorTag, errorMessage, null, null, errorPath);
87 * Constructs a RestConfError object.
90 * The enumerated type indicating the layer where the error occurred.
92 * The enumerated tag representing a more specific error cause.
94 * A string which provides a plain text string describing the error.
96 * A string which represents an application-specific error tag that further specifies the error cause.
98 * A string, <b>formatted as XML</b>, which contains additional error information.
100 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
101 final String errorAppTag, final String errorInfo) {
102 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
106 * Constructs a RestConfError object.
109 * The enumerated type indicating the layer where the error occurred.
111 * The enumerated tag representing a more specific error cause.
112 * @param errorMessage
113 * A string which provides a plain text string describing the error.
115 * A string which represents an application-specific error tag that further specifies the error cause.
117 * A string, <b>formatted as XML</b>, which contains additional error information.
119 * An instance identifier which contains error path
121 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
122 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
123 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
124 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
125 this.errorMessage = errorMessage;
126 this.errorAppTag = errorAppTag;
127 this.errorInfo = errorInfo;
128 this.errorPath = errorPath;
132 * Constructs a RestConfError object from an RpcError.
134 public RestconfError(final RpcError rpcError) {
136 errorType = rpcError.getErrorType();
138 final ErrorTag tag = rpcError.getTag();
139 errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
141 errorMessage = rpcError.getMessage();
142 errorAppTag = rpcError.getApplicationTag();
144 String localErrorInfo = null;
145 if (rpcError.getInfo() == null) {
146 if (rpcError.getCause() != null) {
147 localErrorInfo = rpcError.getCause().getMessage();
148 } else if (rpcError.getSeverity() != null) {
149 localErrorInfo = "<severity>" + rpcError.getSeverity().elementBody() + "</severity>";
152 localErrorInfo = rpcError.getInfo();
155 errorInfo = localErrorInfo;
159 public ErrorType getErrorType() {
163 public ErrorTag getErrorTag() {
167 public String getErrorInfo() {
171 public String getErrorAppTag() {
175 public String getErrorMessage() {
179 public YangInstanceIdentifier getErrorPath() {
184 public String toString() {
185 return "RestconfError ["
186 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
187 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
188 + (errorMessage != null ? ", error-message: " + errorMessage : "")
189 + (errorInfo != null ? ", error-info: " + errorInfo : "")
190 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")