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.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.ErrorTag;
15 import org.opendaylight.yangtools.yang.common.ErrorType;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 * Encapsulates a single {@code error} within the
21 * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.9">"errors" YANG Data Template</a>.
26 public class RestconfError implements Serializable {
28 private static final long serialVersionUID = 1L;
30 private final @NonNull ErrorType errorType;
31 private final @NonNull ErrorTag errorTag;
32 // FIXME: 'errorInfo' is defined as a formatted XML string. We need a better representation to enable reasonable
33 // interop with JSON (and others)
34 private final String errorInfo;
35 private final String errorAppTag;
36 private final String errorMessage;
37 private final YangInstanceIdentifier errorPath;
40 * Constructs a RestconfError.
42 * @param errorType The enumerated type indicating the layer where the error occurred.
43 * @param errorTag The enumerated tag representing a more specific error cause.
44 * @param errorMessage A string which provides a plain text string describing the error.
46 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
47 this(errorType, errorTag, errorMessage, null, null, null);
51 * Constructs a RestconfError object.
53 * @param errorType The enumerated type indicating the layer where the error occurred.
54 * @param errorTag The enumerated tag representing a more specific error cause.
55 * @param errorMessage A string which provides a plain text string describing the error.
56 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
59 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
60 final String errorAppTag) {
61 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
65 * Constructs a RestconfError object.
67 * @param errorType The enumerated type indicating the layer where the error occurred.
68 * @param errorTag The enumerated tag representing a more specific error cause.
69 * @param errorMessage A string which provides a plain text string describing the error.
70 * @param errorPath An instance identifier which contains error path
72 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
73 final YangInstanceIdentifier errorPath) {
74 this(errorType, errorTag, errorMessage, null, null, errorPath);
78 * Constructs a RestconfError object.
80 * @param errorType The enumerated type indicating the layer where the error occurred.
81 * @param errorTag The enumerated tag representing a more specific error cause.
82 * @param errorMessage A string which provides a plain text string describing the error.
83 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
85 * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
87 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
88 final String errorAppTag, final String errorInfo) {
89 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
93 * Constructs a RestConfError object.
95 * @param errorType The enumerated type indicating the layer where the error occurred.
96 * @param errorTag The enumerated tag representing a more specific error cause.
97 * @param errorMessage A string which provides a plain text string describing the error.
98 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
100 * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
101 * @param errorPath An instance identifier which contains error path
103 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
104 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
105 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
106 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
107 this.errorMessage = errorMessage;
108 this.errorAppTag = errorAppTag;
109 this.errorInfo = errorInfo;
110 this.errorPath = errorPath;
114 * Constructs a RestConfError object from an RpcError.
116 public RestconfError(final RpcError rpcError) {
117 errorType = rpcError.getErrorType();
119 final var tag = rpcError.getTag();
120 errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
122 errorMessage = rpcError.getMessage();
123 errorAppTag = rpcError.getApplicationTag();
124 errorInfo = rpcErrorInfo(rpcError);
128 private static String rpcErrorInfo(final RpcError rpcError) {
129 final var info = rpcError.getInfo();
133 final var cause = rpcError.getCause();
135 return cause.getMessage();
137 final var severity = rpcError.getSeverity();
138 if (severity != null) {
139 return "<severity>" + severity.elementBody() + "</severity>";
144 public @NonNull ErrorType getErrorType() {
148 public @NonNull ErrorTag getErrorTag() {
152 public String getErrorInfo() {
156 public String getErrorAppTag() {
160 public String getErrorMessage() {
164 public YangInstanceIdentifier getErrorPath() {
169 public String toString() {
170 return "RestconfError ["
171 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
172 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
173 + (errorMessage != null ? ", error-message: " + errorMessage : "")
174 + (errorInfo != null ? ", error-info: " + errorInfo : "")
175 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")