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 java.util.List;
14 import javax.ws.rs.WebApplicationException;
15 import javax.ws.rs.core.Response.Status;
16 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
17 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
20 * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
24 * @author Thomas Pantelis
25 * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
27 public class RestconfDocumentedException extends WebApplicationException {
29 private static final long serialVersionUID = 1L;
31 private final List<RestconfError> errors;
32 private final Status status;
35 * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
36 * to OPERATION_FAILED.
39 * A string which provides a plain text string describing the error.
41 public RestconfDocumentedException(String message) {
42 this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
46 * Constructs an instance with an error message, error type, and error tag.
49 * A string which provides a plain text string describing the error.
51 * The enumerated type indicating the layer where the error occurred.
53 * The enumerated tag representing a more specific error cause.
55 public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
56 this(null, new RestconfError(errorType, errorTag, message));
60 * Constructs an instance with an error message and exception cause. The stack trace of the exception is included in
64 * A string which provides a plain text string describing the error.
66 * The underlying exception cause.
68 public RestconfDocumentedException(String message, Throwable cause) {
69 this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
70 message, null, RestconfError.toErrorInfo(cause)));
74 * Constructs an instance with the given error.
76 public RestconfDocumentedException(RestconfError error) {
81 * Constructs an instance with the given errors.
83 public RestconfDocumentedException(List<RestconfError> errors) {
84 this.errors = ImmutableList.copyOf(errors);
85 Preconditions.checkArgument(!this.errors.isEmpty(), "RestconfError list can't be empty");
90 * Constructs an instance with an HTTP status and no error information.
95 public RestconfDocumentedException(Status status) {
96 Preconditions.checkNotNull(status, "Status can't be null");
97 errors = ImmutableList.of();
101 private RestconfDocumentedException(Throwable cause, RestconfError error) {
103 Preconditions.checkNotNull(error, "RestconfError can't be null");
104 errors = ImmutableList.of(error);
108 public List<RestconfError> getErrors() {
112 public Status getStatus() {
117 public String getMessage() {
118 return "errors: " + errors + (status != null ? ", status: " + status : "");