blob: 459531f992b7468ebeff56370bd3d763df21a7e1 [file] [log] [blame]
Jin Gan79f75372017-01-05 15:08:11 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jin Gan79f75372017-01-05 15:08:11 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.restconf.api;
17
Sean Condon13b16812018-01-25 10:31:49 +000018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
Jin Gan79f75372017-01-05 15:08:11 -080022import javax.ws.rs.WebApplicationException;
23import javax.ws.rs.core.Response;
24
Sean Condon13b16812018-01-25 10:31:49 +000025import java.util.ArrayList;
26import java.util.Optional;
27
Jin Gan79f75372017-01-05 15:08:11 -080028import static javax.ws.rs.core.Response.Status;
29
30/**
Sean Condon13b16812018-01-25 10:31:49 +000031 * Exceptions raised during RESTCONF operations. This class extends WebApplicationException.
32 * To comply with the RESTCONF specification on error handling, the parameters given
33 * when creating this exception will be used to create one
34 * {@link org.onosproject.restconf.api.RestconfError}. Additional
35 * {@link org.onosproject.restconf.api.RestconfError}s may be added subsequently.
36 * This exception should be converted to a Json object using the toRestconfError()
37 * method before being passed back to the caller in a response
Jin Gan79f75372017-01-05 15:08:11 -080038 */
39public class RestconfException extends WebApplicationException {
40
41 // This is a randomly generated value. A WebApplicationException class is required to define it.
Sean Condon13b16812018-01-25 10:31:49 +000042 private static final long SERIAL_VERSION_UID = 1275970654684007046L;
43
44 private ArrayList<RestconfError> restconfErrors = new ArrayList<>();
Jin Gan79f75372017-01-05 15:08:11 -080045
46 /**
Sean Condon13b16812018-01-25 10:31:49 +000047 * Constructs a new RESTCONF server error exception based of an existing exception.
48 * The caller raising this exception may pass in a HTTP error status code
49 * and an error message. The error code will be displayed to the RESTCONF
50 * client as part of the response from the RESTCONF server. The error message
51 * is a string which may be saved in a log file and may be later retrieved by
52 * the getMessage() method. The parameters given will be formed in to a
53 * {@link org.onosproject.restconf.api.RestconfError}. Additional errors may be
54 * added after construction.
Jin Gan79f75372017-01-05 15:08:11 -080055 *
56 * @param message the detailed error message
Sean Condon13b16812018-01-25 10:31:49 +000057 * @param throwable The existing exception that caused this response.
58 * The message from this exception will be used as error-info
59 * @param errorTag A Restconf Error tag
60 * @param status HTTP error status. Developers are asked to ensure that the correct one for the error-tag is used
61 * @param errorPath An optional path that gives the item that caused the exception
Jin Gan79f75372017-01-05 15:08:11 -080062 * @throws IllegalArgumentException in case the status code is null or is not from
63 * javax.ws.rs.core.Response.Status.Family
64 * status code family
65 */
Sean Condon13b16812018-01-25 10:31:49 +000066 public RestconfException(String message, Throwable throwable,
67 RestconfError.ErrorTag errorTag, Status status,
68 Optional<String> errorPath) {
69 super(message, throwable, Response.status(status).build());
70 RestconfError.Builder bldr = RestconfError
71 .builder(RestconfError.ErrorType.APPLICATION, errorTag)
72 .errorMessage(message);
73 if (throwable != null) {
74 bldr.errorInfo(throwable.getMessage());
75 }
76 if (errorPath.isPresent()) {
77 bldr.errorPath(errorPath.get());
78 }
79 addToErrors(bldr.build());
Jin Gan79f75372017-01-05 15:08:11 -080080 }
81
82 /**
Sean Condon13b16812018-01-25 10:31:49 +000083 * Constructs a new RESTCONF server error exception.
84 * The caller raising this exception may pass in a HTTP error status code
85 * and an error message. The error code will be displayed to the RESTCONF
86 * client as part of the response from the RESTCONF server. The error message
87 * is a string which may be saved in a log file and may be later retrieved by
88 * the getMessage() method. The parameters given will be formed in to a
89 * {@link org.onosproject.restconf.api.RestconfError}. Additional errors may be
90 * added after construction.
Jin Gan79f75372017-01-05 15:08:11 -080091 *
Sean Condon13b16812018-01-25 10:31:49 +000092 * @param message the detailed error message
93 * @param errorTag A Restconf Error tag
94 * @param status HTTP error status. Developers are asked to ensure that the correct one for the error-tag is used
95 * @param errorPath An optional path that gives the item that caused the exception
96 * @param errorInfo An optional string with more info about the error
97 * @throws IllegalArgumentException in case the status code is null or is not from
Jin Gan79f75372017-01-05 15:08:11 -080098 * javax.ws.rs.core.Response.Status.Family
99 * status code family
100 */
Sean Condon13b16812018-01-25 10:31:49 +0000101 public RestconfException(String message, RestconfError.ErrorTag errorTag,
102 Status status, Optional<String> errorPath,
103 Optional<String> errorInfo) {
104 super(message, null, Response.status(status).build());
105 RestconfError.Builder bldr = RestconfError
106 .builder(RestconfError.ErrorType.APPLICATION, errorTag)
107 .errorMessage(message);
108 if (errorInfo.isPresent()) {
109 bldr.errorInfo(errorInfo.get());
110 }
111 if (errorPath.isPresent()) {
112 bldr.errorPath(errorPath.get());
113 }
114 addToErrors(bldr.build());
115 }
116
117 /**
118 * Allows additional RestconfErrors to be added to the exception.
119 * @param restconfError An additional RestconfError to be added to the response
120 */
121 public void addToErrors(RestconfError restconfError) {
122 restconfErrors.add(restconfError);
123 }
124
125 /**
126 * Convert the RestconfException and all of its RestconfErrors in to a Json object.
127 * @return A json node generated from the RestconfException
128 */
129 public ObjectNode toRestconfErrorJson() {
130 ObjectMapper mapper = new ObjectMapper();
131 ArrayNode errorArray = mapper.createArrayNode();
132 restconfErrors.forEach(error -> errorArray.add(error.toJson()));
133 ObjectNode errorsNode = (ObjectNode) mapper.createObjectNode();
134 errorsNode.put("ietf-restconf:errors", errorArray);
135 return errorsNode;
Jin Gan79f75372017-01-05 15:08:11 -0800136 }
137}