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 com.google.common.collect.Lists;
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.RpcError;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
28 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
31 * @author Thomas Pantelis
33 public class RestconfDocumentedException extends WebApplicationException {
35 private static final long serialVersionUID = 1L;
37 private final ImmutableList<RestconfError> errors;
38 private final Status status;
41 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
42 * to OPERATION_FAILED.
45 * A string which provides a plain text string describing the error.
47 public RestconfDocumentedException(final String message) {
48 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
52 * Constructs an instance with an error message, error type, error tag and exception cause.
55 * A string which provides a plain text string describing the error.
57 * The enumerated type indicating the layer where the error occurred.
59 * The enumerated tag representing a more specific error cause.
61 * The underlying exception cause.
63 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
64 final Throwable cause) {
65 this(cause, new RestconfError(errorType, errorTag, message, null,
66 cause.getMessage(), null));
70 * Constructs an instance with an error message, error type, and error tag.
73 * A string which provides a plain text string describing the error.
75 * The enumerated type indicating the layer where the error occurred.
77 * The enumerated tag representing a more specific error cause.
79 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
80 this(null, new RestconfError(errorType, errorTag, message));
84 * Constructs an instance with an error message, error type, error tag and error path.
87 * A string which provides a plain text string describing the error.
89 * The enumerated type indicating the layer where the error occurred.
91 * The enumerated tag representing a more specific error cause.
93 * The instance identifier representing error path
95 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
96 final YangInstanceIdentifier errorPath) {
97 this(null, new RestconfError(errorType, errorTag, message, errorPath));
101 * Constructs an instance with an error message and exception cause.
102 * The underlying exception is included in the error-info.
105 * A string which provides a plain text string describing the error.
107 * The underlying exception cause.
109 public RestconfDocumentedException(final String message, final Throwable cause) {
110 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
111 message, null, cause.getMessage(), null));
115 * Constructs an instance with the given error.
117 public RestconfDocumentedException(final RestconfError error) {
122 * Constructs an instance with the given errors.
124 public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
125 // FIXME: We override getMessage so supplied message is lost for any public access
126 // this was lost also in original code.
128 if (!errors.isEmpty()) {
129 this.errors = ImmutableList.copyOf(errors);
131 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
132 RestconfError.ErrorTag.OPERATION_FAILED, message));
139 * Constructs an instance with the given RpcErrors.
141 public RestconfDocumentedException(final String message, final Throwable cause,
142 final Collection<? extends RpcError> rpcErrors) {
143 this(message, cause, convertToRestconfErrors(rpcErrors));
147 * Constructs an instance with an HTTP status and no error information.
152 public RestconfDocumentedException(final Status status) {
153 Preconditions.checkNotNull(status, "Status can't be null");
154 errors = ImmutableList.of();
155 this.status = status;
158 public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
160 Preconditions.checkNotNull(error, "RestconfError can't be null");
161 errors = ImmutableList.of(error);
165 private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
166 final List<RestconfError> errorList = Lists.newArrayList();
167 if (rpcErrors != null) {
168 for (RpcError rpcError : rpcErrors) {
169 errorList.add(new RestconfError(rpcError));
176 public List<RestconfError> getErrors() {
180 public Status getStatus() {
185 public String getMessage() {
186 return "errors: " + errors + (status != null ? ", status: " + status : "");