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.netconf.sal.restconf.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Lists;
15 import java.util.Collection;
16 import java.util.List;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.Response.Status;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcError;
24 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
28 * @author Thomas Pantelis
29 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
31 public class RestconfDocumentedException extends WebApplicationException {
33 private static final long serialVersionUID = 1L;
35 private final List<RestconfError> errors;
36 private final Status status;
39 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
40 * to OPERATION_FAILED.
43 * A string which provides a plain text string describing the error.
45 public RestconfDocumentedException(String message) {
46 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
50 * Constructs an instance with an error message, error type, error tag and exception cause.
53 * A string which provides a plain text string describing the error.
55 * The enumerated type indicating the layer where the error occurred.
57 * The enumerated tag representing a more specific error cause.
59 * The underlying exception cause.
61 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag, Throwable cause) {
62 this(cause, new RestconfError(errorType, errorTag, message, null, Throwables.getStackTraceAsString(cause)));
66 * Constructs an instance with an error message, error type, and error tag.
69 * A string which provides a plain text string describing the error.
71 * The enumerated type indicating the layer where the error occurred.
73 * The enumerated tag representing a more specific error cause.
75 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
76 this(null, new RestconfError(errorType, errorTag, message));
80 * Constructs an instance with an error message and exception cause.
81 * The stack trace of the exception is included in the error info.
84 * A string which provides a plain text string describing the error.
86 * The underlying exception cause.
88 public RestconfDocumentedException(String message, Throwable cause) {
89 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
90 message, null, Throwables.getStackTraceAsString(cause)));
94 * Constructs an instance with the given error.
96 public RestconfDocumentedException(RestconfError error) {
101 * Constructs an instance with the given errors.
103 public RestconfDocumentedException(String message, Throwable cause, List<RestconfError> errors) {
104 // FIXME: We override getMessage so supplied message is lost for any public access
105 // this was lost also in original code.
107 if(!errors.isEmpty()) {
108 this.errors = ImmutableList.copyOf(errors);
110 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
111 RestconfError.ErrorTag.OPERATION_FAILED, message));
118 * Constructs an instance with the given RpcErrors.
120 public RestconfDocumentedException(String message, Throwable cause, Collection<RpcError> rpcErrors) {
121 this(message, cause, convertToRestconfErrors(rpcErrors));
125 * Constructs an instance with an HTTP status and no error information.
130 public RestconfDocumentedException(Status status) {
131 Preconditions.checkNotNull(status, "Status can't be null");
132 errors = ImmutableList.of();
133 this.status = status;
136 private RestconfDocumentedException(Throwable cause, RestconfError error) {
138 Preconditions.checkNotNull(error, "RestconfError can't be null");
139 errors = ImmutableList.of(error);
143 private static List<RestconfError> convertToRestconfErrors(Collection<RpcError> rpcErrors) {
144 List<RestconfError> errorList = Lists.newArrayList();
145 if(rpcErrors != null) {
146 for (RpcError rpcError : rpcErrors) {
147 errorList.add(new RestconfError(rpcError));
155 public List<RestconfError> getErrors() {
159 public Status getStatus() {
164 public String getMessage() {
165 return "errors: " + errors + (status != null ? ", status: " + status : "");