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.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.netconf.sal.restconf.impl.RestconfError.ErrorTag;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.RpcError;
23 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
27 * @author Thomas Pantelis
28 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
30 public class RestconfDocumentedException extends WebApplicationException {
32 private static final long serialVersionUID = 1L;
34 private final List<RestconfError> errors;
35 private final Status status;
38 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
39 * to OPERATION_FAILED.
42 * A string which provides a plain text string describing the error.
44 public RestconfDocumentedException(String message) {
45 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
49 * Constructs an instance with an error message, error type, error tag and exception cause.
52 * A string which provides a plain text string describing the error.
54 * The enumerated type indicating the layer where the error occurred.
56 * The enumerated tag representing a more specific error cause.
58 * The underlying exception cause.
60 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag, Throwable cause) {
61 this(cause, new RestconfError(errorType, errorTag, message, null, RestconfError.toErrorInfo(cause)));
65 * Constructs an instance with an error message, error type, and error tag.
68 * A string which provides a plain text string describing the error.
70 * The enumerated type indicating the layer where the error occurred.
72 * The enumerated tag representing a more specific error cause.
74 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
75 this(null, new RestconfError(errorType, errorTag, message));
79 * Constructs an instance with an error message and exception cause.
80 * The stack trace of the exception is included in the error info.
83 * A string which provides a plain text string describing the error.
85 * The underlying exception cause.
87 public RestconfDocumentedException(String message, Throwable cause) {
88 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
89 message, null, RestconfError.toErrorInfo(cause)));
93 * Constructs an instance with the given error.
95 public RestconfDocumentedException(RestconfError error) {
100 * Constructs an instance with the given errors.
102 public RestconfDocumentedException(String message, Throwable cause, List<RestconfError> errors) {
103 // FIXME: We override getMessage so supplied message is lost for any public access
104 // this was lost also in original code.
106 if(!errors.isEmpty()) {
107 this.errors = ImmutableList.copyOf(errors);
109 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
110 RestconfError.ErrorTag.OPERATION_FAILED, message));
117 * Constructs an instance with the given RpcErrors.
119 public RestconfDocumentedException(String message, Throwable cause, Collection<RpcError> rpcErrors) {
120 this(message, cause, convertToRestconfErrors(rpcErrors));
124 * Constructs an instance with an HTTP status and no error information.
129 public RestconfDocumentedException(Status status) {
130 Preconditions.checkNotNull(status, "Status can't be null");
131 errors = ImmutableList.of();
132 this.status = status;
135 private RestconfDocumentedException(Throwable cause, RestconfError error) {
137 Preconditions.checkNotNull(error, "RestconfError can't be null");
138 errors = ImmutableList.of(error);
142 private static List<RestconfError> convertToRestconfErrors(Collection<RpcError> rpcErrors) {
143 List<RestconfError> errorList = Lists.newArrayList();
144 if(rpcErrors != null) {
145 for (RpcError rpcError : rpcErrors) {
146 errorList.add(new RestconfError(rpcError));
154 public List<RestconfError> getErrors() {
158 public Status getStatus() {
163 public String getMessage() {
164 return "errors: " + errors + (status != null ? ", status: " + status : "");