Apply style rules on whole sal-rest-connector
[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 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;
18
19 /**
20  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
21  * client.
22  *
23  * @author Devin Avery
24  * @author Thomas Pantelis
25  * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
26  */
27 public class RestconfDocumentedException extends WebApplicationException {
28
29     private static final long serialVersionUID = 1L;
30
31     private final List<RestconfError> errors;
32     private final Status status;
33
34     /**
35      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
36      * to OPERATION_FAILED.
37      *
38      * @param message
39      *            A string which provides a plain text string describing the error.
40      */
41     public RestconfDocumentedException(String message) {
42         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
43     }
44
45     /**
46      * Constructs an instance with an error message, error type, and error tag.
47      *
48      * @param message
49      *            A string which provides a plain text string describing the error.
50      * @param errorType
51      *            The enumerated type indicating the layer where the error occurred.
52      * @param errorTag
53      *            The enumerated tag representing a more specific error cause.
54      */
55     public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
56         this(null, new RestconfError(errorType, errorTag, message));
57     }
58
59     /**
60      * Constructs an instance with an error message and exception cause. The stack trace of the exception is included in
61      * the error info.
62      *
63      * @param message
64      *            A string which provides a plain text string describing the error.
65      * @param cause
66      *            The underlying exception cause.
67      */
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)));
71     }
72
73     /**
74      * Constructs an instance with the given error.
75      */
76     public RestconfDocumentedException(RestconfError error) {
77         this(null, error);
78     }
79
80     /**
81      * Constructs an instance with the given errors.
82      */
83     public RestconfDocumentedException(List<RestconfError> errors) {
84         this.errors = ImmutableList.copyOf(errors);
85         Preconditions.checkArgument(!this.errors.isEmpty(), "RestconfError list can't be empty");
86         status = null;
87     }
88
89     /**
90      * Constructs an instance with an HTTP status and no error information.
91      *
92      * @param status
93      *            the HTTP status.
94      */
95     public RestconfDocumentedException(Status status) {
96         Preconditions.checkNotNull(status, "Status can't be null");
97         errors = ImmutableList.of();
98         this.status = status;
99     }
100
101     private RestconfDocumentedException(Throwable cause, RestconfError error) {
102         super(cause);
103         Preconditions.checkNotNull(error, "RestconfError can't be null");
104         errors = ImmutableList.of(error);
105         status = null;
106     }
107
108     public List<RestconfError> getErrors() {
109         return errors;
110     }
111
112     public Status getStatus() {
113         return status;
114     }
115
116     @Override
117     public String getMessage() {
118         return "errors: " + errors + (status != null ? ", status: " + status : "");
119     }
120 }