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
9 package org.opendaylight.restconf.common.errors;
11 import com.google.common.base.Preconditions;
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.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.OperationFailedException;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
29 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
32 * @author Thomas Pantelis
34 public class RestconfDocumentedException extends WebApplicationException {
36 private static final long serialVersionUID = 1L;
38 private final ImmutableList<RestconfError> errors;
39 private final Status status;
42 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
43 * to OPERATION_FAILED.
46 * A string which provides a plain text string describing the error.
48 public RestconfDocumentedException(final String message) {
49 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
53 * Constructs an instance with an error message, error type, error tag and exception cause.
56 * A string which provides a plain text string describing the error.
58 * The enumerated type indicating the layer where the error occurred.
60 * The enumerated tag representing a more specific error cause.
62 * The underlying exception cause.
64 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
65 final Throwable cause) {
66 this(cause, new RestconfError(errorType, errorTag, message, null,
67 cause.getMessage(), null));
71 * Constructs an instance with an error message, error type, and error tag.
74 * A string which provides a plain text string describing the error.
76 * The enumerated type indicating the layer where the error occurred.
78 * The enumerated tag representing a more specific error cause.
80 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
81 this(null, new RestconfError(errorType, errorTag, message));
85 * Constructs an instance with an error message, error type, error tag and error path.
88 * A string which provides a plain text string describing the error.
90 * The enumerated type indicating the layer where the error occurred.
92 * The enumerated tag representing a more specific error cause.
94 * The instance identifier representing error path
96 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
97 final YangInstanceIdentifier errorPath) {
98 this(null, new RestconfError(errorType, errorTag, message, errorPath));
102 * Constructs an instance with an error message and exception cause.
103 * The underlying exception is included in the error-info.
106 * A string which provides a plain text string describing the error.
108 * The underlying exception cause.
110 public RestconfDocumentedException(final String message, final Throwable cause) {
111 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
112 message, null, cause.getMessage(), null));
116 * Constructs an instance with the given error.
118 public RestconfDocumentedException(final RestconfError error) {
123 * Constructs an instance with the given errors.
125 public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
126 // FIXME: We override getMessage so supplied message is lost for any public access
127 // this was lost also in original code.
129 if (!errors.isEmpty()) {
130 this.errors = ImmutableList.copyOf(errors);
132 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
133 RestconfError.ErrorTag.OPERATION_FAILED, message));
140 * Constructs an instance with the given RpcErrors.
142 public RestconfDocumentedException(final String message, final Throwable cause,
143 final Collection<? extends RpcError> rpcErrors) {
144 this(message, cause, convertToRestconfErrors(rpcErrors));
148 * Constructs an instance with an HTTP status and no error information.
153 public RestconfDocumentedException(final Status status) {
154 Preconditions.checkNotNull(status, "Status can't be null");
155 errors = ImmutableList.of();
156 this.status = status;
159 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
160 super(cause, error.getErrorTag().getStatusCode());
161 Preconditions.checkNotNull(error, "RestconfError can't be null");
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());
178 private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
179 final List<RestconfError> errorList = new ArrayList<>();
180 if (rpcErrors != null) {
181 for (RpcError rpcError : rpcErrors) {
182 errorList.add(new RestconfError(rpcError));
189 public List<RestconfError> getErrors() {
193 public Status getStatus() {
198 public String getMessage() {
199 return "errors: " + errors + (status != null ? ", status: " + status : "");