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>.
25 public class RestconfError implements Serializable {
27 private static final long serialVersionUID = 1L;
29 private final @NonNull ErrorType errorType;
30 private final @NonNull ErrorTag errorTag;
31 // FIXME: 'errorInfo' is defined as a formatted XML string. We need a better representation to enable reasonable
32 // interop with JSON (and others)
33 private final String errorInfo;
34 private final String errorAppTag;
35 private final String errorMessage;
36 private final YangInstanceIdentifier errorPath;
39 * Constructs a RestconfError.
41 * @param errorType The enumerated type indicating the layer where the error occurred.
42 * @param errorTag The enumerated tag representing a more specific error cause.
43 * @param errorMessage A string which provides a plain text string describing the error.
45 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
46 this(errorType, errorTag, errorMessage, null, null, null);
50 * Constructs a RestconfError object.
52 * @param errorType The enumerated type indicating the layer where the error occurred.
53 * @param errorTag The enumerated tag representing a more specific error cause.
54 * @param errorMessage A string which provides a plain text string describing the error.
55 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
58 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
59 final String errorAppTag) {
60 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
64 * Constructs a RestconfError object.
66 * @param errorType The enumerated type indicating the layer where the error occurred.
67 * @param errorTag The enumerated tag representing a more specific error cause.
68 * @param errorMessage A string which provides a plain text string describing the error.
69 * @param errorPath An instance identifier which contains error path
71 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
72 final YangInstanceIdentifier errorPath) {
73 this(errorType, errorTag, errorMessage, null, null, errorPath);
77 * Constructs a RestconfError object.
79 * @param errorType The enumerated type indicating the layer where the error occurred.
80 * @param errorTag The enumerated tag representing a more specific error cause.
81 * @param errorMessage A string which provides a plain text string describing the error.
82 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
84 * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
86 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
87 final String errorAppTag, final String errorInfo) {
88 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
92 * Constructs a RestConfError object.
94 * @param errorType The enumerated type indicating the layer where the error occurred.
95 * @param errorTag The enumerated tag representing a more specific error cause.
96 * @param errorMessage A string which provides a plain text string describing the error.
97 * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
99 * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
100 * @param errorPath An instance identifier which contains error path
102 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
103 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
104 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
105 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
106 this.errorMessage = errorMessage;
107 this.errorAppTag = errorAppTag;
108 this.errorInfo = errorInfo;
109 this.errorPath = errorPath;
113 * Constructs a RestConfError object from an RpcError.
115 public RestconfError(final RpcError rpcError) {
116 errorType = rpcError.getErrorType();
118 final var tag = rpcError.getTag();
119 errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
121 errorMessage = rpcError.getMessage();
122 errorAppTag = rpcError.getApplicationTag();
123 errorInfo = rpcErrorInfo(rpcError);
127 private static String rpcErrorInfo(final RpcError rpcError) {
128 final var info = rpcError.getInfo();
132 final var cause = rpcError.getCause();
134 return cause.getMessage();
136 final var severity = rpcError.getSeverity();
137 if (severity != null) {
138 return "<severity>" + severity.elementBody() + "</severity>";
143 public @NonNull ErrorType getErrorType() {
147 public @NonNull ErrorTag getErrorTag() {
151 public String getErrorInfo() {
155 public String getErrorAppTag() {
159 public String getErrorMessage() {
163 public YangInstanceIdentifier getErrorPath() {
168 public String toString() {
169 return "RestconfError ["
170 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
171 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
172 + (errorMessage != null ? ", error-message: " + errorMessage : "")
173 + (errorInfo != null ? ", error-info: " + errorInfo : "")
174 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")