/
JsonException.java
202 lines (187 loc) · 6.88 KB
/
JsonException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package jmri.server.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Throw an exception, but include an HTTP error code.
*
* @author Randall Wood Copyright (C) 2015, 2016
*/
public class JsonException extends Exception {
/* JSON protocol keys */
/**
* {@value #ERROR}
*/
public static final String ERROR = "error"; // NOI18N
/**
* {@value #CODE}
*/
public static final String CODE = "code"; // NOI18N
/**
* {@value #MESSAGE}
*/
public static final String MESSAGE = "message"; // NOI18N
/* Error message localization keys */
/**
* {@value #ERROR_BAD_PROPERTY_VALUE}, a key for localized error messages
* indicating the value for the given property is not valid.
*/
public static final String ERROR_BAD_PROPERTY_VALUE = "ErrorBadPropertyValue"; // NOI18N
/**
* {@value #ERROR_MISSING_PROPERTY_PUT}, a key for localized error messages
* indicating a property required to complete a PUT request is missing.
*/
public static final String ERROR_MISSING_PROPERTY_PUT = "ErrorMissingPropertyPut"; // NOI18N
/**
* {@value #ERROR_NOT_FOUND}, a key for localized error messages indicating
* a resource was not found.
*/
public static final String ERROR_NOT_FOUND = "ErrorNotFound"; // NOI18N
/**
* {@value #ERROR_OBJECT}, a key for localized error messages indicating
* an inability to get a requested object.
*/
public static final String ERROR_OBJECT = "ErrorObject"; // NOI18N
/**
* {@value #ERROR_UNKNOWN_TYPE}, a key for localized error messages
* indicating the requested type cannot be handled.
*/
public static final String ERROR_UNKNOWN_TYPE = "ErrorUnknownType"; // NOI18N
/**
* {@value #LOGGED_ERROR}, a key for localized error messages indicating
* that JMRI logs contain required information to resolve the error.
*/
public static final String LOGGED_ERROR = "LoggedError"; // NOI18N
/* Internal objects */
private static final ObjectMapper MAPPER = new ObjectMapper();
private final int errorCode;
private final transient ObjectNode additionalData;
/**
* Only access through {@link #getId()}, even when used within this class
*/
private final int id;
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param message message, displayable to the user, in the client's
* preferred locale
* @param throwable the cause of the exception
* @param id the message id passed by the client, or the additive
* inverse of that id
*/
public JsonException(int code, String message, Throwable throwable, int id) {
this(code, message, throwable, null, id);
}
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param message message, displayable to the user, in the client's
* preferred locale
* @param throwable the cause of the exception
* @param additionalData additional data to be passed to the client
* @param id the message id passed by the client, or the
* additive inverse of that id
*/
public JsonException(int code, String message, Throwable throwable, ObjectNode additionalData, int id) {
super(message, throwable);
this.errorCode = code;
this.additionalData = additionalData;
this.id = id;
}
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param throwable the cause of the exception
* @param id the message id passed by the client, or the additive
* inverse of that id
*/
public JsonException(int code, Throwable throwable, int id) {
this(code, throwable, null, id);
}
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param throwable the cause of the exception
* @param additionalData additional data to be passed to the client
* @param id the message id passed by the client, or the
* additive inverse of that id
*/
public JsonException(int code, Throwable throwable, ObjectNode additionalData, int id) {
super(throwable);
this.errorCode = code;
this.additionalData = additionalData;
this.id = id;
}
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param message message, displayable to the user, in the client's
* preferred locale
* @param id the message id passed by the client, or the additive
* inverse of that id
*/
public JsonException(int code, String message, int id) {
this(code, message, (ObjectNode) null, id);
}
/**
* Create an exception that can be passed to a JSON client.
*
* @param code the error code
* @param message message, displayable to the user, in the client's
* preferred locale
* @param additionalData additional data to be passed to the client
* @param id the message id passed by the client, or the
* additive inverse of that id
*/
public JsonException(int code, String message, ObjectNode additionalData, int id) {
super(message);
this.errorCode = code;
this.additionalData = additionalData;
this.id = id;
}
/**
* Get the error code (usually an HTTP error code)
*
* @return the code
*/
public int getCode() {
return this.errorCode;
}
/**
* Get any additional data passed to the client. This is specific to, and
* will vary based on, the original exception.
*
* @return the additional data or null if none
*/
public ObjectNode getAdditionalData() {
return this.additionalData.deepCopy();
}
/**
* Get the id passed to the client.
*
* @return the absolute value of the id
* @see JsonHttpService
*/
public int getId() {
return Math.abs(id);
}
/**
* Get the JSON formatted error message.
*
* @return the error message in a JSON format
*/
public JsonNode getJsonMessage() {
ObjectNode data = MAPPER.createObjectNode();
data.put(CODE, this.getCode());
data.put(MESSAGE, this.getMessage());
if (additionalData != null) {
data.setAll(additionalData);
}
return JsonHttpService.message(MAPPER, ERROR, data, null, getId());
}
}