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.data.api.YangInstanceIdentifier;
27 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
31 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
34 * @author Thomas Pantelis
36 public class RestconfDocumentedException extends WebApplicationException {
38 private static final long serialVersionUID = 1L;
40 private final ImmutableList<RestconfError> errors;
41 private final Status status;
44 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
45 * to OPERATION_FAILED.
48 * A string which provides a plain text string describing the error.
50 public RestconfDocumentedException(final String message) {
51 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
55 * Constructs an instance with an error message, error type, error tag and exception cause.
58 * A string which provides a plain text string describing the error.
60 * The enumerated type indicating the layer where the error occurred.
62 * The enumerated tag representing a more specific error cause.
64 * The underlying exception cause.
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));
73 * Constructs an instance with an error message, error type, and error tag.
76 * A string which provides a plain text string describing the error.
78 * The enumerated type indicating the layer where the error occurred.
80 * The enumerated tag representing a more specific error cause.
82 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
83 this(null, new RestconfError(errorType, errorTag, message));
87 * Constructs an instance with an error message, error type, error tag and error path.
90 * A string which provides a plain text string describing the error.
92 * The enumerated type indicating the layer where the error occurred.
94 * The enumerated tag representing a more specific error cause.
96 * The instance identifier representing error path
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));
104 * Constructs an instance with an error message and exception cause.
105 * The underlying exception is included in the error-info.
108 * A string which provides a plain text string describing the error.
110 * The underlying exception cause.
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));
118 * Constructs an instance with the given error.
120 public RestconfDocumentedException(final RestconfError error) {
125 * Constructs an instance with the given errors.
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.
131 if (!errors.isEmpty()) {
132 this.errors = ImmutableList.copyOf(errors);
134 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
135 RestconfError.ErrorTag.OPERATION_FAILED, message));
142 * Constructs an instance with the given RpcErrors.
144 public RestconfDocumentedException(final String message, final Throwable cause,
145 final Collection<? extends RpcError> rpcErrors) {
146 this(message, cause, convertToRestconfErrors(rpcErrors));
150 * Constructs an instance with an HTTP status and no error information.
155 public RestconfDocumentedException(final Status status) {
156 errors = ImmutableList.of();
157 this.status = requireNonNull(status, "Status can't be null");
160 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
161 super(cause, error.getErrorTag().getStatusCode());
162 errors = ImmutableList.of(error);
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);
175 throw new RestconfDocumentedException(message, cause, cause.getErrorList());
179 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
180 * this method does nothing.
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.
189 public static void throwIf(final boolean expression, final ErrorType errorType, final ErrorTag errorTag,
190 final @NonNull String format, final Object... args) {
192 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
197 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
198 * this method does nothing.
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.
206 public static void throwIf(final boolean expression, final @NonNull String message,
207 final ErrorType errorType, final ErrorTag errorTag) {
209 throw new RestconfDocumentedException(message, errorType, errorTag);
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.
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.
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) {
227 throw new RestconfDocumentedException(String.format(format, args), errorType, errorTag);
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));
243 public List<RestconfError> getErrors() {
247 public Status getStatus() {
252 public String getMessage() {
253 return "errors: " + errors + (status != null ? ", status: " + status : "");