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.controller.sal.restconf.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Lists;
15 import java.util.Collection;
16 import java.util.List;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.Response.Status;
21 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
22 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
23 import org.opendaylight.yangtools.yang.common.RpcError;
26 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
30 * @author Thomas Pantelis
31 * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
33 public class RestconfDocumentedException extends WebApplicationException {
35 private static final long serialVersionUID = 1L;
37 private final List<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(String message) {
48 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
52 * Constructs an instance with an error message, error type, and error tag.
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 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
62 this(null, new RestconfError(errorType, errorTag, message));
66 * Constructs an instance with an error message and exception cause.
67 * The stack trace of the exception is included in the error info.
70 * A string which provides a plain text string describing the error.
72 * The underlying exception cause.
74 public RestconfDocumentedException(String message, Throwable cause) {
75 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
76 message, null, RestconfError.toErrorInfo(cause)));
80 * Constructs an instance with the given error.
82 public RestconfDocumentedException(RestconfError error) {
87 * Constructs an instance with the given errors.
89 public RestconfDocumentedException(String message, Throwable cause, List<RestconfError> errors) {
90 // FIXME: We override getMessage so supplied message is lost for any public access
91 // this was lost also in original code.
93 if(!errors.isEmpty()) {
94 this.errors = ImmutableList.copyOf(errors);
96 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
97 RestconfError.ErrorTag.OPERATION_FAILED, message));
104 * Constructs an instance with the given RpcErrors.
106 public RestconfDocumentedException(String message, Throwable cause, Collection<RpcError> rpcErrors) {
107 this(message, cause, convertToRestconfErrors(rpcErrors));
111 * Constructs an instance with an HTTP status and no error information.
116 public RestconfDocumentedException(Status status) {
117 Preconditions.checkNotNull(status, "Status can't be null");
118 errors = ImmutableList.of();
119 this.status = status;
122 private RestconfDocumentedException(Throwable cause, RestconfError error) {
124 Preconditions.checkNotNull(error, "RestconfError can't be null");
125 errors = ImmutableList.of(error);
129 private static List<RestconfError> convertToRestconfErrors(Collection<RpcError> rpcErrors) {
130 List<RestconfError> errorList = Lists.newArrayList();
131 if(rpcErrors != null) {
132 for (RpcError rpcError : rpcErrors) {
133 errorList.add(new RestconfError(rpcError));
141 public List<RestconfError> getErrors() {
145 public Status getStatus() {
150 public String getMessage() {
151 return "errors: " + errors + (status != null ? ", status: " + status : "");