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;
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 * @author Thomas Pantelis
30 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
32 public class RestconfDocumentedException extends WebApplicationException {
34 private static final long serialVersionUID = 1L;
36 private final List<RestconfError> errors;
37 private final Status status;
40 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
41 * to OPERATION_FAILED.
44 * A string which provides a plain text string describing the error.
46 public RestconfDocumentedException(final String message) {
47 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
51 * Constructs an instance with an error message, error type, error tag and exception cause.
54 * A string which provides a plain text string describing the error.
56 * The enumerated type indicating the layer where the error occurred.
58 * The enumerated tag representing a more specific error cause.
60 * The underlying exception cause.
62 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
63 final Throwable cause) {
64 this(cause, new RestconfError(errorType, errorTag, message, null,
65 Throwables.getStackTraceAsString(cause), null));
69 * Constructs an instance with an error message, error type, and error tag.
72 * A string which provides a plain text string describing the error.
74 * The enumerated type indicating the layer where the error occurred.
76 * The enumerated tag representing a more specific error cause.
78 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
79 this(null, new RestconfError(errorType, errorTag, message));
83 * Constructs an instance with an error message, error type, error tag and error path.
86 * A string which provides a plain text string describing the error.
88 * The enumerated type indicating the layer where the error occurred.
90 * The enumerated tag representing a more specific error cause.
92 * The instance identifier representing error path
94 public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
95 final YangInstanceIdentifier errorPath) {
96 this(null, new RestconfError(errorType, errorTag, message, errorPath));
100 * Constructs an instance with an error message and exception cause.
101 * The stack trace of the exception is included in the error info.
104 * A string which provides a plain text string describing the error.
106 * The underlying exception cause.
108 public RestconfDocumentedException(final String message, final Throwable cause) {
109 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
110 message, null, Throwables.getStackTraceAsString(cause), null));
114 * Constructs an instance with the given error.
116 public RestconfDocumentedException(final RestconfError error) {
121 * Constructs an instance with the given errors.
123 public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
124 // FIXME: We override getMessage so supplied message is lost for any public access
125 // this was lost also in original code.
127 if(!errors.isEmpty()) {
128 this.errors = ImmutableList.copyOf(errors);
130 this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
131 RestconfError.ErrorTag.OPERATION_FAILED, message));
138 * Constructs an instance with the given RpcErrors.
140 public RestconfDocumentedException(final String message, final Throwable cause,
141 final Collection<RpcError> rpcErrors) {
142 this(message, cause, convertToRestconfErrors(rpcErrors));
146 * Constructs an instance with an HTTP status and no error information.
151 public RestconfDocumentedException(final Status status) {
152 Preconditions.checkNotNull(status, "Status can't be null");
153 errors = ImmutableList.of();
154 this.status = status;
157 private RestconfDocumentedException(final Throwable cause, final RestconfError error) {
159 Preconditions.checkNotNull(error, "RestconfError can't be null");
160 errors = ImmutableList.of(error);
164 private static List<RestconfError> convertToRestconfErrors(final Collection<RpcError> rpcErrors) {
165 final List<RestconfError> errorList = Lists.newArrayList();
166 if(rpcErrors != null) {
167 for (RpcError rpcError : rpcErrors) {
168 errorList.add(new RestconfError(rpcError));
175 public List<RestconfError> getErrors() {
179 public Status getStatus() {
184 public String getMessage() {
185 return "errors: " + errors + (status != null ? ", status: " + status : "");