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.Serial;
13 import java.io.Serializable;
14 import java.util.Locale;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
17 import org.opendaylight.yangtools.yang.common.RpcError;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 * Encapsulates a restconf error as defined in the ietf restconf draft.
25 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
28 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
30 public class RestconfError implements Serializable {
32 private static final long serialVersionUID = 1L;
34 private final ErrorType errorType;
35 private final ErrorTag errorTag;
36 private final String errorInfo;
37 private final String errorAppTag;
38 private final String errorMessage;
39 private final YangInstanceIdentifier errorPath;
42 * Constructs a RestConfError.
45 * The enumerated type indicating the layer where the error occurred.
47 * The enumerated tag representing a more specific error cause.
49 * A string which provides a plain text string describing the error.
51 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
52 this(errorType, errorTag, errorMessage, null, null, null);
56 * Constructs a RestConfError object.
59 * The enumerated type indicating the layer where the error occurred.
61 * The enumerated tag representing a more specific error cause.
63 * A string which provides a plain text string describing the error.
65 * A string which represents an application-specific error tag that further specifies the error cause.
67 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
68 final String errorAppTag) {
69 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
73 * Constructs a RestConfError object.
76 * The enumerated type indicating the layer where the error occurred.
78 * The enumerated tag representing a more specific error cause.
80 * A string which provides a plain text string describing the error.
82 * An instance identifier which contains error path
84 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
85 final YangInstanceIdentifier errorPath) {
86 this(errorType, errorTag, errorMessage, null, null, errorPath);
90 * Constructs a RestConfError object.
93 * The enumerated type indicating the layer where the error occurred.
95 * The enumerated tag representing a more specific error cause.
97 * A string which provides a plain text string describing the error.
99 * A string which represents an application-specific error tag that further specifies the error cause.
101 * A string, <b>formatted as XML</b>, which contains additional error information.
103 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
104 final String errorAppTag, final String errorInfo) {
105 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
109 * Constructs a RestConfError object.
112 * The enumerated type indicating the layer where the error occurred.
114 * The enumerated tag representing a more specific error cause.
115 * @param errorMessage
116 * A string which provides a plain text string describing the error.
118 * A string which represents an application-specific error tag that further specifies the error cause.
120 * A string, <b>formatted as XML</b>, which contains additional error information.
122 * An instance identifier which contains error path
124 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
125 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
126 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
127 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
128 this.errorMessage = errorMessage;
129 this.errorAppTag = errorAppTag;
130 this.errorInfo = errorInfo;
131 this.errorPath = errorPath;
135 * Constructs a RestConfError object from an RpcError.
137 public RestconfError(final RpcError rpcError) {
139 errorType = rpcError.getErrorType();
141 final ErrorTag tag = rpcError.getTag();
142 errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
144 errorMessage = rpcError.getMessage();
145 errorAppTag = rpcError.getApplicationTag();
147 String localErrorInfo = null;
148 if (rpcError.getInfo() == null) {
149 if (rpcError.getCause() != null) {
150 localErrorInfo = rpcError.getCause().getMessage();
151 } else if (rpcError.getSeverity() != null) {
152 localErrorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase(Locale.ROOT)
156 localErrorInfo = rpcError.getInfo();
159 errorInfo = localErrorInfo;
163 public ErrorType getErrorType() {
167 public ErrorTag getErrorTag() {
171 public String getErrorInfo() {
175 public String getErrorAppTag() {
179 public String getErrorMessage() {
183 public YangInstanceIdentifier getErrorPath() {
188 public String toString() {
189 return "RestconfError ["
190 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
191 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
192 + (errorMessage != null ? ", error-message: " + errorMessage : "")
193 + (errorInfo != null ? ", error-info: " + errorInfo : "")
194 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")