23
23
import org .restheart .utils .HttpStatus ;
24
24
25
25
/**
26
+ * Exception thrown when a client request is malformed or contains invalid data.
27
+ * <p>
28
+ * This exception is used throughout the RESTHeart framework to signal various
29
+ * types of client errors, typically resulting in HTTP 4xx status codes. It provides
30
+ * flexibility in specifying custom status codes, message formats, and content types
31
+ * for error responses.
32
+ * </p>
33
+ * <p>
34
+ * The exception supports both plain text and JSON error messages, allowing services
35
+ * to return structured error information when appropriate.
36
+ * </p>
26
37
*
27
38
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
28
39
*/
29
40
public class BadRequestException extends RuntimeException {
30
- /**
31
- *
32
- */
41
+ /** Serial version UID for serialization compatibility. */
33
42
private static final long serialVersionUID = -8466126772297299751L ;
34
43
44
+ /** The HTTP status code to be returned with this exception. */
35
45
int statusCode = HttpStatus .SC_BAD_REQUEST ;
46
+
47
+ /** Flag indicating whether the exception message is valid JSON. */
36
48
private final boolean jsonMessage ;
49
+
50
+ /** The content type to be used in the error response. */
37
51
private final String contentType ;
38
52
39
53
/**
40
- *
54
+ * Creates a new BadRequestException with default settings.
55
+ * <p>
56
+ * Uses HTTP 400 (Bad Request) status code, plain text message format,
57
+ * and JSON content type.
58
+ * </p>
41
59
*/
42
60
public BadRequestException () {
43
61
super ();
@@ -46,8 +64,12 @@ public BadRequestException() {
46
64
}
47
65
48
66
/**
67
+ * Creates a new BadRequestException with a custom HTTP status code.
68
+ * <p>
69
+ * Uses plain text message format and JSON content type.
70
+ * </p>
49
71
*
50
- * @param statusCode
72
+ * @param statusCode the HTTP status code to return with this exception
51
73
*/
52
74
public BadRequestException (int statusCode ) {
53
75
super ();
@@ -57,8 +79,13 @@ public BadRequestException(int statusCode) {
57
79
}
58
80
59
81
/**
82
+ * Creates a new BadRequestException with a custom error message.
83
+ * <p>
84
+ * Uses HTTP 400 (Bad Request) status code, plain text message format,
85
+ * and JSON content type.
86
+ * </p>
60
87
*
61
- * @param message
88
+ * @param message the error message to include in the exception
62
89
*/
63
90
public BadRequestException (String message ) {
64
91
super (message );
@@ -67,9 +94,13 @@ public BadRequestException(String message) {
67
94
}
68
95
69
96
/**
97
+ * Creates a new BadRequestException with a custom error message and message format.
98
+ * <p>
99
+ * Uses HTTP 400 (Bad Request) status code and JSON content type.
100
+ * </p>
70
101
*
71
- * @param message
72
- * @param jsonMessage mark message as json, i.e. the string is valid json
102
+ * @param message the error message to include in the exception
103
+ * @param jsonMessage true if the message is valid JSON, false for plain text
73
104
*/
74
105
public BadRequestException (String message , boolean jsonMessage ) {
75
106
super (message );
@@ -78,10 +109,14 @@ public BadRequestException(String message, boolean jsonMessage) {
78
109
}
79
110
80
111
/**
112
+ * Creates a new BadRequestException with custom message, format, and content type.
113
+ * <p>
114
+ * Uses HTTP 400 (Bad Request) status code.
115
+ * </p>
81
116
*
82
- * @param message
83
- * @param jsonMessage mark message as json, i.e. the string is valid json
84
- * @param contentType error response content type
117
+ * @param message the error message to include in the exception
118
+ * @param jsonMessage true if the message is valid JSON, false for plain text
119
+ * @param contentType the content type to use in the error response
85
120
*/
86
121
public BadRequestException (String message , boolean jsonMessage , String contentType ) {
87
122
super (message );
@@ -90,9 +125,13 @@ public BadRequestException(String message, boolean jsonMessage, String contentTy
90
125
}
91
126
92
127
/**
128
+ * Creates a new BadRequestException with custom message and status code.
129
+ * <p>
130
+ * Uses plain text message format and JSON content type.
131
+ * </p>
93
132
*
94
- * @param message
95
- * @param statusCode
133
+ * @param message the error message to include in the exception
134
+ * @param statusCode the HTTP status code to return with this exception
96
135
*/
97
136
public BadRequestException (String message , int statusCode ) {
98
137
super (message );
@@ -102,10 +141,14 @@ public BadRequestException(String message, int statusCode) {
102
141
}
103
142
104
143
/**
144
+ * Creates a new BadRequestException with custom message, status code, and message format.
145
+ * <p>
146
+ * Uses JSON content type.
147
+ * </p>
105
148
*
106
- * @param message
107
- * @param statusCode
108
- * @param jsonMessage mark message as json, i.e. the string is valid json
149
+ * @param message the error message to include in the exception
150
+ * @param statusCode the HTTP status code to return with this exception
151
+ * @param jsonMessage true if the message is valid JSON, false for plain text
109
152
*/
110
153
public BadRequestException (String message , int statusCode , boolean jsonMessage ) {
111
154
super (message );
@@ -115,11 +158,15 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage)
115
158
}
116
159
117
160
/**
161
+ * Creates a new BadRequestException with full customization options.
162
+ * <p>
163
+ * This constructor provides complete control over all exception properties.
164
+ * </p>
118
165
*
119
- * @param message
120
- * @param statusCode
121
- * @param jsonMessage mark message as json, i.e. the string is valid json
122
- * @param contentType error response content type
166
+ * @param message the error message to include in the exception
167
+ * @param statusCode the HTTP status code to return with this exception
168
+ * @param jsonMessage true if the message is valid JSON, false for plain text
169
+ * @param contentType the content type to use in the error response
123
170
*/
124
171
public BadRequestException (String message , int statusCode , boolean jsonMessage , String contentType ) {
125
172
super (message );
@@ -129,9 +176,14 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage,
129
176
}
130
177
131
178
/**
179
+ * Creates a new BadRequestException with a message and underlying cause.
180
+ * <p>
181
+ * Uses HTTP 400 (Bad Request) status code, plain text message format,
182
+ * and plain text content type.
183
+ * </p>
132
184
*
133
- * @param message
134
- * @param cause
185
+ * @param message the error message to include in the exception
186
+ * @param cause the underlying cause of this exception
135
187
*/
136
188
public BadRequestException (String message , Throwable cause ) {
137
189
super (message , cause );
@@ -140,10 +192,14 @@ public BadRequestException(String message, Throwable cause) {
140
192
}
141
193
142
194
/**
195
+ * Creates a new BadRequestException with message, cause, and message format.
196
+ * <p>
197
+ * Uses HTTP 400 (Bad Request) status code and JSON content type.
198
+ * </p>
143
199
*
144
- * @param message
145
- * @param cause
146
- * @param jsonMessage mark message as json, i.e. the string is valid json
200
+ * @param message the error message to include in the exception
201
+ * @param jsonMessage true if the message is valid JSON, false for plain text
202
+ * @param cause the underlying cause of this exception
147
203
*/
148
204
public BadRequestException (String message , boolean jsonMessage , Throwable cause ) {
149
205
super (message , cause );
@@ -152,11 +208,15 @@ public BadRequestException(String message, boolean jsonMessage, Throwable cause)
152
208
}
153
209
154
210
/**
211
+ * Creates a new BadRequestException with message, cause, format, and content type.
212
+ * <p>
213
+ * Uses HTTP 400 (Bad Request) status code.
214
+ * </p>
155
215
*
156
- * @param message
157
- * @param cause
158
- * @param jsonMessage mark message as json, i.e. the string is valid json
159
- * @param contentType error response content type
216
+ * @param message the error message to include in the exception
217
+ * @param jsonMessage true if the message is valid JSON, false for plain text
218
+ * @param contentType the content type to use in the error response
219
+ * @param cause the underlying cause of this exception
160
220
*/
161
221
public BadRequestException (String message , boolean jsonMessage , String contentType , Throwable cause ) {
162
222
super (message , cause );
@@ -165,10 +225,14 @@ public BadRequestException(String message, boolean jsonMessage, String contentTy
165
225
}
166
226
167
227
/**
228
+ * Creates a new BadRequestException with message, status code, and cause.
229
+ * <p>
230
+ * Uses plain text message format and JSON content type.
231
+ * </p>
168
232
*
169
- * @param message
170
- * @param statusCode
171
- * @param cause
233
+ * @param message the error message to include in the exception
234
+ * @param statusCode the HTTP status code to return with this exception
235
+ * @param cause the underlying cause of this exception
172
236
*/
173
237
public BadRequestException (String message , int statusCode , Throwable cause ) {
174
238
super (message , cause );
@@ -178,11 +242,15 @@ public BadRequestException(String message, int statusCode, Throwable cause) {
178
242
}
179
243
180
244
/**
245
+ * Creates a new BadRequestException with message, status code, format, and cause.
246
+ * <p>
247
+ * Uses JSON content type.
248
+ * </p>
181
249
*
182
- * @param message
183
- * @param statusCode
184
- * @param jsonMessage mark message as json, i.e. the string is valid json
185
- * @param cause
250
+ * @param message the error message to include in the exception
251
+ * @param statusCode the HTTP status code to return with this exception
252
+ * @param jsonMessage true if the message is valid JSON, false for plain text
253
+ * @param cause the underlying cause of this exception
186
254
*/
187
255
public BadRequestException (String message , int statusCode , boolean jsonMessage , Throwable cause ) {
188
256
super (message , cause );
@@ -192,12 +260,17 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage,
192
260
}
193
261
194
262
/**
263
+ * Creates a new BadRequestException with full customization and a cause.
264
+ * <p>
265
+ * This constructor provides complete control over all exception properties
266
+ * while also capturing the underlying cause of the error.
267
+ * </p>
195
268
*
196
- * @param message
197
- * @param statusCode
198
- * @param jsonMessage mark message as json, i.e. the string is valid json
199
- * @param contentType error response content type
200
- * @param cause
269
+ * @param message the error message to include in the exception
270
+ * @param statusCode the HTTP status code to return with this exception
271
+ * @param jsonMessage true if the message is valid JSON, false for plain text
272
+ * @param contentType the content type to use in the error response
273
+ * @param cause the underlying cause of this exception
201
274
*/
202
275
public BadRequestException (String message , int statusCode , boolean jsonMessage , String contentType , Throwable cause ) {
203
276
super (message , cause );
@@ -207,16 +280,36 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage,
207
280
}
208
281
209
282
/**
210
- * @return the statusCode
283
+ * Returns the HTTP status code associated with this exception.
284
+ *
285
+ * @return the HTTP status code to be returned in the error response
211
286
*/
212
287
public int getStatusCode () {
213
288
return statusCode ;
214
289
}
215
290
291
+ /**
292
+ * Indicates whether the exception message is formatted as valid JSON.
293
+ * <p>
294
+ * When true, the message can be directly used as JSON content in the response.
295
+ * When false, the message should be treated as plain text.
296
+ * </p>
297
+ *
298
+ * @return true if the message is valid JSON, false otherwise
299
+ */
216
300
public boolean isJsonMessage () {
217
301
return jsonMessage ;
218
302
}
219
303
304
+ /**
305
+ * Returns the content type to be used in the error response.
306
+ * <p>
307
+ * This determines how the client should interpret the error message content.
308
+ * Common values include "application/json" and "text/plain".
309
+ * </p>
310
+ *
311
+ * @return the content type for the error response
312
+ */
220
313
public String contentType () {
221
314
return this .contentType ;
222
315
}
0 commit comments