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 java.util.List;
13 import javax.ws.rs.WebApplicationException;
14 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;
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
23 * Unchecked exception to communicate error information, as defined in the ietf restcong draft,
24 * to be sent to the client.
27 * @author Thomas Pantelis
28 * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
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
39 * the error tag defaults to OPERATION_FAILED.
41 * @param message A string which provides a plain text string describing the error.
43 public RestconfDocumentedException( String message ) {
44 this( message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED );
48 * Constructs an instance with an error message, error type, and error tag.
50 * @param message A string which provides a plain text string describing the error.
51 * @param errorType The enumerated type indicating the layer where the error occurred.
52 * @param errorTag The enumerated tag representing a more specific error cause.
54 public RestconfDocumentedException( String message, ErrorType errorType, ErrorTag errorTag ) {
55 this( null, new RestconfError( errorType, errorTag, message ) );
59 * Constructs an instance with an error message and exception cause. The stack trace of the
60 * exception is included in the error info.
62 * @param message A string which provides a plain text string describing the error.
63 * @param cause The underlying exception cause.
65 public RestconfDocumentedException( String message, Throwable cause ) {
66 this( cause, new RestconfError( RestconfError.ErrorType.APPLICATION,
67 RestconfError.ErrorTag.OPERATION_FAILED, message,
68 null, RestconfError.toErrorInfo( cause ) ) );
72 * Constructs an instance with the given error.
74 public RestconfDocumentedException( RestconfError error ) {
79 * Constructs an instance with the given errors.
81 public RestconfDocumentedException( List<RestconfError> errors ) {
82 this.errors = ImmutableList.copyOf( errors );
83 Preconditions.checkArgument( !this.errors.isEmpty(), "RestconfError list can't be empty" );
88 * Constructs an instance with an HTTP status and no error information.
90 * @param status the HTTP status.
92 public RestconfDocumentedException( Status status ) {
93 Preconditions.checkNotNull( status, "Status can't be null" );
94 errors = ImmutableList.of();
98 private RestconfDocumentedException( Throwable cause, RestconfError error ) {
100 Preconditions.checkNotNull( error, "RestconfError can't be null" );
101 errors = ImmutableList.of( error );
105 public List<RestconfError> getErrors() {
109 public Status getStatus() {
115 public String getMessage() {
116 return "errors: " + errors + (status != null ? ", status: " + status : "");