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.yangtools.yang.common.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, 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, cause.getMessage(), null));
74 * Constructs an instance with an error message, error type, and error tag.
77 * A string which provides a plain text string describing the error.
79 * The enumerated type indicating the layer where the error occurred.
81 * The enumerated tag representing a more specific error cause.
83 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
84 this(null, new RestconfError(errorType, errorTag, message));
88 * Constructs an instance with an error message, error type, error tag and error path.
91 * A string which provides a plain text string describing the error.
93 * The enumerated type indicating the layer where the error occurred.
95 * The enumerated tag representing a more specific error cause.
97 * The instance identifier representing error path
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));
105 * Constructs an instance with an error message and exception cause.
106 * The underlying exception is included in the error-info.
109 * A string which provides a plain text string describing the error.
111 * The underlying exception cause.
113 public RestconfDocumentedException(final String message, final Throwable cause) {
114 this(cause, new RestconfError(ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
115 message, null, cause.getMessage(), null));
119 * Constructs an instance with the given error.
121 public RestconfDocumentedException(final RestconfError error) {
126 * Constructs an instance with the given errors.
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.
132 if (!errors.isEmpty()) {
133 this.errors = ImmutableList.copyOf(errors);
135 this.errors = ImmutableList.of(new RestconfError(ErrorType.APPLICATION,
136 RestconfError.ErrorTag.OPERATION_FAILED, message));
143 * Constructs an instance with the given RpcErrors.
145 public RestconfDocumentedException(final String message, final Throwable cause,
146 final Collection<? extends RpcError> rpcErrors) {
147 this(message, cause, convertToRestconfErrors(rpcErrors));
151 * Constructs an instance with an HTTP status and no error information.
156 public RestconfDocumentedException(final Status status) {
157 errors = ImmutableList.of();
158 this.status = requireNonNull(status, "Status can't be null");
161 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
162 super(cause, error.getErrorTag().getStatusCode());
163 errors = ImmutableList.of(error);
167 public static RestconfDocumentedException decodeAndThrow(final String message,
168 final OperationFailedException cause) {
169 for (final RpcError error : cause.getErrorList()) {
170 if (error.getErrorType() == RpcError.ErrorType.TRANSPORT
171 && error.getTag().equals(ErrorTag.RESOURCE_DENIED.getTagValue())) {
172 throw new RestconfDocumentedException(error.getMessage(), ErrorType.TRANSPORT,
173 ErrorTag.RESOURCE_DENIED_TRANSPORT, cause);
176 throw new RestconfDocumentedException(message, cause, cause.getErrorList());
180 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
181 * this method does nothing.
183 * @param expression Expression to be evaluated
184 * @param errorType The enumerated type indicating the layer where the error occurred.
185 * @param errorTag The enumerated tag representing a more specific error cause.
186 * @param format Format string, according to {@link String#format(String, Object...)}.
187 * @param args Format string arguments, according to {@link String#format(String, Object...)}
188 * @throws RestconfDocumentedException if the expression evaluates to true.
190 public static void throwIf(final boolean expression, final ErrorType errorType, final ErrorTag errorTag,
191 final @NonNull String format, final Object... args) {
193 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
198 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
199 * this method does nothing.
201 * @param expression Expression to be evaluated
202 * @param message error message
203 * @param errorType The enumerated type indicating the layer where the error occurred.
204 * @param errorTag The enumerated tag representing a more specific error cause.
205 * @throws RestconfDocumentedException if the expression evaluates to true.
207 public static void throwIf(final boolean expression, final @NonNull String message,
208 final ErrorType errorType, final ErrorTag errorTag) {
210 throw new RestconfDocumentedException(message, errorType, errorTag);
215 * Throw an instance of this exception if an object is null. If the object is non-null, it will
216 * be returned as the result of this method.
218 * @param obj Object reference to be checked
219 * @param errorType The enumerated type indicating the layer where the error occurred.
220 * @param errorTag The enumerated tag representing a more specific error cause.
221 * @param format Format string, according to {@link String#format(String, Object...)}.
222 * @param args Format string arguments, according to {@link String#format(String, Object...)}
223 * @throws RestconfDocumentedException if the expression evaluates to true.
225 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final ErrorType errorType, final ErrorTag errorTag,
226 final @NonNull String format, final Object... args) {
228 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
234 * Throw an instance of this exception if the specified exception has a {@link YangError} attachment.
236 * @param cause Proposed cause of a RestconfDocumented exception
238 public static void throwIfYangError(final Throwable cause) {
239 if (cause instanceof YangError) {
240 final YangError error = (YangError) cause;
241 throw new RestconfDocumentedException(cause, new RestconfError(error.getErrorType().toNetconf(),
242 // FIXME: this is a special-case until we have YangError.getTag()
243 cause instanceof YangInvalidValueException ? ErrorTag.INVALID_VALUE : ErrorTag.MALFORMED_MESSAGE,
244 error.getErrorMessage().orElse(null), error.getErrorAppTag().orElse(null)));
248 private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
249 final List<RestconfError> errorList = new ArrayList<>();
250 if (rpcErrors != null) {
251 for (RpcError rpcError : rpcErrors) {
252 errorList.add(new RestconfError(rpcError));
259 public List<RestconfError> getErrors() {
263 public Status getStatus() {
268 public String getMessage() {
269 return "errors: " + errors + (status != null ? ", status: " + status : "");