Bug 1125: Added regression test
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfDocumentedException.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.restconf.impl;
10
11 import java.util.List;
12
13 import javax.ws.rs.WebApplicationException;
14 import javax.ws.rs.core.Response.Status;
15
16 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
17 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
21
22 /**
23  * Unchecked exception to communicate error information, as defined in the ietf restcong draft,
24  * to be sent to the client.
25  *
26  * @author Devin Avery
27  * @author Thomas Pantelis
28  * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
29  */
30 public class RestconfDocumentedException extends WebApplicationException {
31
32     private static final long serialVersionUID = 1L;
33
34     private final List<RestconfError> errors;
35     private final Status status;
36
37     /**
38      * Constructs an instance with an error message. The error type defaults to APPLICATION and
39      * the error tag defaults to OPERATION_FAILED.
40      *
41      * @param message A string which provides a plain text string describing the error.
42      */
43     public RestconfDocumentedException( String message ) {
44         this( message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED );
45     }
46
47     /**
48      * Constructs an instance with an error message, error type, and error tag.
49      *
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.
53      */
54     public RestconfDocumentedException( String message, ErrorType errorType, ErrorTag errorTag ) {
55         this( null, new RestconfError( errorType, errorTag, message ) );
56     }
57
58     /**
59      * Constructs an instance with an error message and exception cause. The stack trace of the
60      * exception is included in the error info.
61      *
62      * @param message A string which provides a plain text string describing the error.
63      * @param cause The underlying exception cause.
64      */
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 ) ) );
69     }
70
71     /**
72      * Constructs an instance with the given error.
73      */
74     public RestconfDocumentedException( RestconfError error ) {
75         this( null, error );
76     }
77
78     /**
79      * Constructs an instance with the given errors.
80      */
81     public RestconfDocumentedException( List<RestconfError> errors ) {
82         this.errors = ImmutableList.copyOf( errors );
83         Preconditions.checkArgument( !this.errors.isEmpty(), "RestconfError list can't be empty" );
84         status = null;
85     }
86
87     /**
88      * Constructs an instance with an HTTP status and no error information.
89      *
90      * @param status the HTTP status.
91      */
92     public RestconfDocumentedException( Status status ) {
93         Preconditions.checkNotNull( status, "Status can't be null" );
94         errors = ImmutableList.of();
95         this.status = status;
96     }
97
98     private RestconfDocumentedException( Throwable cause, RestconfError error ) {
99         super( cause );
100         Preconditions.checkNotNull( error, "RestconfError can't be null" );
101         errors = ImmutableList.of( error );
102         status = null;
103     }
104
105     public List<RestconfError> getErrors() {
106         return errors;
107     }
108
109     public Status getStatus() {
110         return status;
111     }
112
113
114     @Override
115     public String getMessage() {
116         return "errors: " + errors + (status != null ? ", status: " + status : "");
117     }
118 }