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 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;
29 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
33 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
36 * @author Thomas Pantelis
38 public class RestconfDocumentedException extends WebApplicationException {
40 private static final long serialVersionUID = 1L;
42 private final ImmutableList<RestconfError> errors;
43 private final Status status;
46 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
47 * to OPERATION_FAILED.
50 * A string which provides a plain text string describing the error.
52 public RestconfDocumentedException(final String message) {
53 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
57 * Constructs an instance with an error message, error type, error tag and exception cause.
60 * A string which provides a plain text string describing the error.
62 * The enumerated type indicating the layer where the error occurred.
64 * The enumerated tag representing a more specific error cause.
66 * The underlying exception cause.
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));
75 * Constructs an instance with an error message, error type, and error tag.
78 * A string which provides a plain text string describing the error.
80 * The enumerated type indicating the layer where the error occurred.
82 * The enumerated tag representing a more specific error cause.
84 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
85 this(null, new RestconfError(errorType, errorTag, message));
89 * Constructs an instance with an error message, error type, error tag and error path.
92 * A string which provides a plain text string describing the error.
94 * The enumerated type indicating the layer where the error occurred.
96 * The enumerated tag representing a more specific error cause.
98 * The instance identifier representing error path
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));
106 * Constructs an instance with an error message and exception cause.
107 * The underlying exception is included in the error-info.
110 * A string which provides a plain text string describing the error.
112 * The underlying exception cause.
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));
120 * Constructs an instance with the given error.
122 public RestconfDocumentedException(final RestconfError error) {
127 * Constructs an instance with the given errors.
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.
133 if (!errors.isEmpty()) {
134 this.errors = ImmutableList.copyOf(errors);
136 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
137 RestconfError.ErrorTag.OPERATION_FAILED, message));
144 * Constructs an instance with the given RpcErrors.
146 public RestconfDocumentedException(final String message, final Throwable cause,
147 final Collection<? extends RpcError> rpcErrors) {
148 this(message, cause, convertToRestconfErrors(rpcErrors));
152 * Constructs an instance with an HTTP status and no error information.
157 public RestconfDocumentedException(final Status status) {
158 errors = ImmutableList.of();
159 this.status = requireNonNull(status, "Status can't be null");
162 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
163 super(cause, error.getErrorTag().getStatusCode());
164 errors = ImmutableList.of(error);
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);
177 throw new RestconfDocumentedException(message, cause, cause.getErrorList());
181 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
182 * this method does nothing.
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.
191 public static void throwIf(final boolean expression, final ErrorType errorType, final ErrorTag errorTag,
192 final @NonNull String format, final Object... args) {
194 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
199 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
200 * this method does nothing.
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.
208 public static void throwIf(final boolean expression, final @NonNull String message,
209 final ErrorType errorType, final ErrorTag errorTag) {
211 throw new RestconfDocumentedException(message, errorType, errorTag);
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.
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.
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) {
229 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
235 * Throw an instance of this exception if the specified exception has a {@link YangError} attachment.
237 * @param cause Proposed cause of a RestconfDocumented exception
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)));
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));
260 public List<RestconfError> getErrors() {
264 public Status getStatus() {
269 public String getMessage() {
270 return "errors: " + errors + (status != null ? ", status: " + status : "");