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