Update RESTCONF error mapping
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / RestconfDocumentedException.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 com.google.common.collect.ImmutableList;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.Response.Status;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
21 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
22 import org.opendaylight.yangtools.yang.common.OperationFailedException;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.YangError;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.codec.YangInvalidValueException;
27
28 /**
29  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
30  * client.
31  *
32  * <p>
33  * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
34  *
35  * @author Devin Avery
36  * @author Thomas Pantelis
37  */
38 public class RestconfDocumentedException extends WebApplicationException {
39
40     private static final long serialVersionUID = 1L;
41
42     private final ImmutableList<RestconfError> errors;
43     private final Status status;
44
45     /**
46      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
47      * to OPERATION_FAILED.
48      *
49      * @param message
50      *            A string which provides a plain text string describing the error.
51      */
52     public RestconfDocumentedException(final String message) {
53         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
54     }
55
56     /**
57      * Constructs an instance with an error message, error type, error tag and exception cause.
58      *
59      * @param message
60      *            A string which provides a plain text string describing the error.
61      * @param errorType
62      *            The enumerated type indicating the layer where the error occurred.
63      * @param errorTag
64      *            The enumerated tag representing a more specific error cause.
65      * @param cause
66      *            The underlying exception cause.
67      */
68     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
69                                        final Throwable cause) {
70         this(cause, new RestconfError(errorType, errorTag, message, null,
71                 cause.getMessage(), null));
72     }
73
74     /**
75      * Constructs an instance with an error message, error type, and error tag.
76      *
77      * @param message
78      *            A string which provides a plain text string describing the error.
79      * @param errorType
80      *            The enumerated type indicating the layer where the error occurred.
81      * @param errorTag
82      *            The enumerated tag representing a more specific error cause.
83      */
84     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
85         this(null, new RestconfError(errorType, errorTag, message));
86     }
87
88     /**
89      * Constructs an instance with an error message, error type, error tag and error path.
90      *
91      * @param message
92      *            A string which provides a plain text string describing the error.
93      * @param errorType
94      *            The enumerated type indicating the layer where the error occurred.
95      * @param errorTag
96      *            The enumerated tag representing a more specific error cause.
97      * @param errorPath
98      *            The instance identifier representing error path
99      */
100     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
101                                        final YangInstanceIdentifier errorPath) {
102         this(null, new RestconfError(errorType, errorTag, message, errorPath));
103     }
104
105     /**
106      * Constructs an instance with an error message and exception cause.
107      * The underlying exception is included in the error-info.
108      *
109      * @param message
110      *            A string which provides a plain text string describing the error.
111      * @param cause
112      *            The underlying exception cause.
113      */
114     public RestconfDocumentedException(final String message, final Throwable cause) {
115         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
116                 message, null, cause.getMessage(), null));
117     }
118
119     /**
120      * Constructs an instance with the given error.
121      */
122     public RestconfDocumentedException(final RestconfError error) {
123         this(null, error);
124     }
125
126     /**
127      * Constructs an instance with the given errors.
128      */
129     public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
130         // FIXME: We override getMessage so supplied message is lost for any public access
131         // this was lost also in original code.
132         super(cause);
133         if (!errors.isEmpty()) {
134             this.errors = ImmutableList.copyOf(errors);
135         } else {
136             this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
137                     RestconfError.ErrorTag.OPERATION_FAILED, message));
138         }
139
140         status = null;
141     }
142
143     /**
144      * Constructs an instance with the given RpcErrors.
145      */
146     public RestconfDocumentedException(final String message, final Throwable cause,
147                                        final Collection<? extends RpcError> rpcErrors) {
148         this(message, cause, convertToRestconfErrors(rpcErrors));
149     }
150
151     /**
152      * Constructs an instance with an HTTP status and no error information.
153      *
154      * @param status
155      *            the HTTP status.
156      */
157     public RestconfDocumentedException(final Status status) {
158         errors = ImmutableList.of();
159         this.status = requireNonNull(status, "Status can't be null");
160     }
161
162     public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
163         super(cause, error.getErrorTag().getStatusCode());
164         errors = ImmutableList.of(error);
165         status = null;
166     }
167
168     public static RestconfDocumentedException decodeAndThrow(final String message,
169             final OperationFailedException cause) {
170         for (final RpcError error : cause.getErrorList()) {
171             if (error.getErrorType() == RpcError.ErrorType.TRANSPORT
172                     && error.getTag().equals(ErrorTag.RESOURCE_DENIED.getTagValue())) {
173                 throw new RestconfDocumentedException(error.getMessage(), ErrorType.TRANSPORT,
174                     ErrorTag.RESOURCE_DENIED_TRANSPORT, cause);
175             }
176         }
177         throw new RestconfDocumentedException(message, cause, cause.getErrorList());
178     }
179
180     /**
181      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
182      * this method does nothing.
183      *
184      * @param expression Expression to be evaluated
185      * @param errorType The enumerated type indicating the layer where the error occurred.
186      * @param errorTag The enumerated tag representing a more specific error cause.
187      * @param format Format string, according to {@link String#format(String, Object...)}.
188      * @param args Format string arguments, according to {@link String#format(String, Object...)}
189      * @throws RestconfDocumentedException if the expression evaluates to true.
190      */
191     public static void throwIf(final boolean expression, final ErrorType errorType, final ErrorTag errorTag,
192             final @NonNull String format, final Object... args) {
193         if (expression) {
194             throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
195         }
196     }
197
198     /**
199      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
200      * this method does nothing.
201      *
202      * @param expression Expression to be evaluated
203      * @param message error message
204      * @param errorType The enumerated type indicating the layer where the error occurred.
205      * @param errorTag The enumerated tag representing a more specific error cause.
206      * @throws RestconfDocumentedException if the expression evaluates to true.
207      */
208     public static void throwIf(final boolean expression, final @NonNull String message,
209             final ErrorType errorType, final ErrorTag errorTag) {
210         if (expression) {
211             throw new RestconfDocumentedException(message, errorType, errorTag);
212         }
213     }
214
215     /**
216      * Throw an instance of this exception if an object is null. If the object is non-null, it will
217      * be returned as the result of this method.
218      *
219      * @param obj Object reference to be checked
220      * @param errorType The enumerated type indicating the layer where the error occurred.
221      * @param errorTag The enumerated tag representing a more specific error cause.
222      * @param format Format string, according to {@link String#format(String, Object...)}.
223      * @param args Format string arguments, according to {@link String#format(String, Object...)}
224      * @throws RestconfDocumentedException if the expression evaluates to true.
225      */
226     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final ErrorType errorType, final ErrorTag errorTag,
227             final @NonNull String format, final Object... args) {
228         if (obj == null) {
229             throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
230         }
231         return obj;
232     }
233
234     /**
235      * Throw an instance of this exception if the specified exception has a {@link YangError} attachment.
236      *
237      * @param cause Proposed cause of a RestconfDocumented exception
238      */
239     public static void throwIfYangError(final Throwable cause) {
240         if (cause instanceof YangError) {
241             final YangError error = (YangError) cause;
242             throw new RestconfDocumentedException(cause, new RestconfError(ErrorType.valueOf(error.getErrorType()),
243                 // FIXME: this is a special-case until we have YangError.getTag()
244                 cause instanceof YangInvalidValueException ? ErrorTag.INVALID_VALUE : ErrorTag.MALFORMED_MESSAGE,
245                     error.getErrorMessage().orElse(null), error.getErrorAppTag().orElse(null)));
246         }
247     }
248
249     private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
250         final List<RestconfError> errorList = new ArrayList<>();
251         if (rpcErrors != null) {
252             for (RpcError rpcError : rpcErrors) {
253                 errorList.add(new RestconfError(rpcError));
254             }
255         }
256
257         return errorList;
258     }
259
260     public List<RestconfError> getErrors() {
261         return errors;
262     }
263
264     public Status getStatus() {
265         return status;
266     }
267
268     @Override
269     public String getMessage() {
270         return "errors: " + errors + (status != null ? ", status: " + status : "");
271     }
272 }