0d1868e03d3c9a933b9895e21527f0c103e4d1d0
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / 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.restconf.common.errors;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Lists;
14 import java.util.Collection;
15 import java.util.List;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.Response.Status;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22
23 /**
24  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
25  * client.
26  *
27  * <p>
28  * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
29  *
30  * @author Devin Avery
31  * @author Thomas Pantelis
32  */
33 public class RestconfDocumentedException extends WebApplicationException {
34
35     private static final long serialVersionUID = 1L;
36
37     private final ImmutableList<RestconfError> errors;
38     private final Status status;
39
40     /**
41      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
42      * to OPERATION_FAILED.
43      *
44      * @param message
45      *            A string which provides a plain text string describing the error.
46      */
47     public RestconfDocumentedException(final String message) {
48         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
49     }
50
51     /**
52      * Constructs an instance with an error message, error type, error tag and exception cause.
53      *
54      * @param message
55      *            A string which provides a plain text string describing the error.
56      * @param errorType
57      *            The enumerated type indicating the layer where the error occurred.
58      * @param errorTag
59      *            The enumerated tag representing a more specific error cause.
60      * @param cause
61      *            The underlying exception cause.
62      */
63     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
64                                        final Throwable cause) {
65         this(cause, new RestconfError(errorType, errorTag, message, null,
66                 cause.getMessage(), null));
67     }
68
69     /**
70      * Constructs an instance with an error message, error type, and error tag.
71      *
72      * @param message
73      *            A string which provides a plain text string describing the error.
74      * @param errorType
75      *            The enumerated type indicating the layer where the error occurred.
76      * @param errorTag
77      *            The enumerated tag representing a more specific error cause.
78      */
79     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
80         this(null, new RestconfError(errorType, errorTag, message));
81     }
82
83     /**
84      * Constructs an instance with an error message, error type, error tag and error path.
85      *
86      * @param message
87      *            A string which provides a plain text string describing the error.
88      * @param errorType
89      *            The enumerated type indicating the layer where the error occurred.
90      * @param errorTag
91      *            The enumerated tag representing a more specific error cause.
92      * @param errorPath
93      *            The instance identifier representing error path
94      */
95     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
96                                        final YangInstanceIdentifier errorPath) {
97         this(null, new RestconfError(errorType, errorTag, message, errorPath));
98     }
99
100     /**
101      * Constructs an instance with an error message and exception cause.
102      * The underlying exception is included in the error-info.
103      *
104      * @param message
105      *            A string which provides a plain text string describing the error.
106      * @param cause
107      *            The underlying exception cause.
108      */
109     public RestconfDocumentedException(final String message, final Throwable cause) {
110         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
111                 message, null, cause.getMessage(), null));
112     }
113
114     /**
115      * Constructs an instance with the given error.
116      */
117     public RestconfDocumentedException(final RestconfError error) {
118         this(null, error);
119     }
120
121     /**
122      * Constructs an instance with the given errors.
123      */
124     public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
125         // FIXME: We override getMessage so supplied message is lost for any public access
126         // this was lost also in original code.
127         super(cause);
128         if (!errors.isEmpty()) {
129             this.errors = ImmutableList.copyOf(errors);
130         } else {
131             this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
132                     RestconfError.ErrorTag.OPERATION_FAILED, message));
133         }
134
135         status = null;
136     }
137
138     /**
139      * Constructs an instance with the given RpcErrors.
140      */
141     public RestconfDocumentedException(final String message, final Throwable cause,
142                                        final Collection<? extends RpcError> rpcErrors) {
143         this(message, cause, convertToRestconfErrors(rpcErrors));
144     }
145
146     /**
147      * Constructs an instance with an HTTP status and no error information.
148      *
149      * @param status
150      *            the HTTP status.
151      */
152     public RestconfDocumentedException(final Status status) {
153         Preconditions.checkNotNull(status, "Status can't be null");
154         errors = ImmutableList.of();
155         this.status = status;
156     }
157
158     public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
159         super(cause);
160         Preconditions.checkNotNull(error, "RestconfError can't be null");
161         errors = ImmutableList.of(error);
162         status = null;
163     }
164
165     private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
166         final List<RestconfError> errorList = Lists.newArrayList();
167         if (rpcErrors != null) {
168             for (RpcError rpcError : rpcErrors) {
169                 errorList.add(new RestconfError(rpcError));
170             }
171         }
172
173         return errorList;
174     }
175
176     public List<RestconfError> getErrors() {
177         return errors;
178     }
179
180     public Status getStatus() {
181         return status;
182     }
183
184     @Override
185     public String getMessage() {
186         return "errors: " + errors + (status != null ? ", status: " + status : "");
187     }
188 }