@@ -16,18 +16,58 @@ public final class BadRequestException extends RuntimeException {
1616
1717 private static final int DEFAULT_STATUS = 400 ;
1818
19+ /** HTTP 4xx status code carried by this exception. */
1920 private final int status ;
21+
22+ /** JSON Pointer (RFC 6901) to the offending property; may be {@code null}. */
2023 private final String pointer ;
24+
25+ /** Validation keyword that failed (for example {@code "required"}); may be {@code null}. */
2126 private final String keyword ;
2227
28+ /**
29+ * Creates a new exception with the default HTTP status {@code 400 Bad Request}.
30+ *
31+ * <p>Equivalent to {@link #BadRequestException(int, String, String, String)} invoked with {@code
32+ * status = 400} and no pointer or keyword.
33+ *
34+ * @param detail human-readable explanation of the error; surfaced as the RFC 7807 {@code detail}
35+ * member
36+ */
2337 public BadRequestException (String detail ) {
2438 this (DEFAULT_STATUS , detail , null , null );
2539 }
2640
41+ /**
42+ * Creates a new exception with an explicit 4xx HTTP status.
43+ *
44+ * <p>Equivalent to {@link #BadRequestException(int, String, String, String)} with no pointer or
45+ * keyword.
46+ *
47+ * @param status the HTTP status code; must be in the range {@code 400}-{@code 499} or an {@link
48+ * IllegalArgumentException} is thrown
49+ * @param detail human-readable explanation of the error; surfaced as the RFC 7807 {@code detail}
50+ * member
51+ */
2752 public BadRequestException (int status , String detail ) {
2853 this (status , detail , null , null );
2954 }
3055
56+ /**
57+ * Creates a new exception with the full set of RFC 7807 problem fields. Intended for
58+ * validation-style errors where the offending property can be pinpointed with a JSON Pointer and
59+ * the failing rule identified by a JSON Schema keyword.
60+ *
61+ * @param status the HTTP status code; must be in the range {@code 400}-{@code 499}
62+ * @param detail human-readable explanation of the error; surfaced as the RFC 7807 {@code detail}
63+ * member
64+ * @param pointer JSON Pointer (RFC 6901) to the offending property in the request payload; may be
65+ * {@code null} when not applicable
66+ * @param keyword JSON Schema / validation keyword that failed (for example {@code "required"},
67+ * {@code "pattern"}, {@code "maxLength"}); may be {@code null} when not applicable
68+ * @throws NullPointerException if {@code detail} is {@code null}
69+ * @throws IllegalArgumentException if {@code status} is not in the range {@code 400}-{@code 499}
70+ */
3171 public BadRequestException (int status , String detail , String pointer , String keyword ) {
3272 super (Objects .requireNonNull (detail , "detail must not be null" ));
3373 if (status < 400 || status > 499 ) {
@@ -38,14 +78,32 @@ public BadRequestException(int status, String detail, String pointer, String key
3878 this .keyword = keyword ;
3979 }
4080
81+ /**
82+ * Returns the HTTP status code carried by this exception.
83+ *
84+ * @return the HTTP status code; always a 4xx value in the range {@code 400}-{@code 499}
85+ */
4186 public int status () {
4287 return status ;
4388 }
4489
90+ /**
91+ * Returns the JSON Pointer locating the offending property in the request payload.
92+ *
93+ * @return an {@link Optional} containing the JSON Pointer (RFC 6901) to the offending property,
94+ * or {@link Optional#empty()} when no pointer was supplied
95+ */
4596 public Optional <String > pointer () {
4697 return Optional .ofNullable (pointer );
4798 }
4899
100+ /**
101+ * Returns the JSON Schema / validation keyword that failed.
102+ *
103+ * @return an {@link Optional} containing the failing JSON Schema or validation keyword (for
104+ * example {@code "required"}, {@code "pattern"}), or {@link Optional#empty()} when no keyword
105+ * was supplied
106+ */
49107 public Optional <String > keyword () {
50108 return Optional .ofNullable (keyword );
51109 }
0 commit comments