Clean up RestconfError
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / RestconfError.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.common.errors;
9
10 import static java.util.Objects.requireNonNull;
11
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;
18
19 /**
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>.
22  *
23  * @author Devin Avery
24  */
25 public class RestconfError implements Serializable {
26     @java.io.Serial
27     private static final long serialVersionUID = 1L;
28
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;
37
38     /**
39      * Constructs a RestconfError.
40      *
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.
44      */
45     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
46         this(errorType, errorTag, errorMessage, null, null, null);
47     }
48
49     /**
50      * Constructs a RestconfError object.
51      *
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
56      *                    cause.
57      */
58     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
59             final String errorAppTag) {
60         this(errorType, errorTag, errorMessage, errorAppTag, null, null);
61     }
62
63     /**
64      * Constructs a RestconfError object.
65      *
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
70      */
71     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
72             final YangInstanceIdentifier errorPath) {
73         this(errorType, errorTag, errorMessage, null, null, errorPath);
74     }
75
76     /**
77      * Constructs a RestconfError object.
78      *
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
83      *                    cause.
84      * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
85      */
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);
89     }
90
91     /**
92      * Constructs a RestConfError object.
93      *
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
98      *                    cause.
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
101      */
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;
110     }
111
112     /**
113      * Constructs a RestConfError object from an RpcError.
114      */
115     public RestconfError(final RpcError rpcError) {
116         errorType = rpcError.getErrorType();
117
118         final var tag = rpcError.getTag();
119         errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
120
121         errorMessage = rpcError.getMessage();
122         errorAppTag = rpcError.getApplicationTag();
123         errorInfo = rpcErrorInfo(rpcError);
124         errorPath = null;
125     }
126
127     private static String rpcErrorInfo(final RpcError rpcError) {
128         final var info = rpcError.getInfo();
129         if (info != null) {
130             return info;
131         }
132         final var cause = rpcError.getCause();
133         if (cause != null) {
134             return cause.getMessage();
135         }
136         final var severity = rpcError.getSeverity();
137         if (severity != null) {
138             return "<severity>" + severity.elementBody() + "</severity>";
139         }
140         return null;
141     }
142
143     public @NonNull ErrorType getErrorType() {
144         return errorType;
145     }
146
147     public @NonNull ErrorTag getErrorTag() {
148         return errorTag;
149     }
150
151     public String getErrorInfo() {
152         return errorInfo;
153     }
154
155     public String getErrorAppTag() {
156         return errorAppTag;
157     }
158
159     public String getErrorMessage() {
160         return errorMessage;
161     }
162
163     public YangInstanceIdentifier getErrorPath() {
164         return errorPath;
165     }
166
167     @Override
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() : "")
175                 + "]";
176     }
177 }