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