Skip to content

Commit e491c28

Browse files
committed
Start work on #508, new exception type
1 parent 04bba39 commit e491c28

File tree

4 files changed

+200
-50
lines changed

4 files changed

+200
-50
lines changed

src/main/java/com/fasterxml/jackson/core/JsonParseException.java

+20-50
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,21 @@
55

66
package com.fasterxml.jackson.core;
77

8+
import com.fasterxml.jackson.core.exc.JsonReadException;
89
import com.fasterxml.jackson.core.util.RequestPayload;
910

1011
/**
1112
* Exception type for parsing problems, used when non-well-formed content
1213
* (content that does not conform to JSON syntax as per specification)
1314
* is encountered.
1415
*/
15-
public class JsonParseException extends JsonProcessingException {
16+
public class JsonParseException extends JsonReadException
17+
{
1618
private static final long serialVersionUID = 2L; // 2.7
1719

18-
// transient since 2.7.4
19-
protected transient JsonParser _processor;
20-
21-
/**
22-
* Optional payload that can be assigned to pass along for error reporting
23-
* or handling purposes. Core streaming parser implementations DO NOT
24-
* initialize this; it is up to using applications and frameworks to
25-
* populate it.
26-
*
27-
* @since 2.8
28-
*/
29-
protected RequestPayload _requestPayload;
30-
3120
@Deprecated // since 2.7
3221
public JsonParseException(String msg, JsonLocation loc) {
33-
super(msg, loc);
22+
super(msg, loc, null);
3423
}
3524

3625
@Deprecated // since 2.7
@@ -46,32 +35,28 @@ public JsonParseException(String msg, JsonLocation loc, Throwable root) {
4635
* @since 2.7
4736
*/
4837
public JsonParseException(JsonParser p, String msg) {
49-
super(msg, (p == null) ? null : p.getCurrentLocation());
50-
_processor = p;
38+
super(p, msg);
5139
}
5240

5341
/**
5442
* @since 2.7
5543
*/
5644
public JsonParseException(JsonParser p, String msg, Throwable root) {
57-
super(msg, (p == null) ? null : p.getCurrentLocation(), root);
58-
_processor = p;
45+
super(p, msg, root);
5946
}
6047

6148
/**
6249
* @since 2.7
6350
*/
6451
public JsonParseException(JsonParser p, String msg, JsonLocation loc) {
65-
super(msg, loc);
66-
_processor = p;
52+
super(p, msg, loc);
6753
}
6854

6955
/**
7056
* @since 2.7
7157
*/
7258
public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root) {
7359
super(msg, loc, root);
74-
_processor = p;
7560
}
7661

7762
/**
@@ -82,6 +67,7 @@ public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable
8267
*
8368
* @since 2.7
8469
*/
70+
@Override
8571
public JsonParseException withParser(JsonParser p) {
8672
_processor = p;
8773
return this;
@@ -95,49 +81,33 @@ public JsonParseException withParser(JsonParser p) {
9581
*
9682
* @since 2.8
9783
*/
84+
@Override
9885
public JsonParseException withRequestPayload(RequestPayload p) {
9986
_requestPayload = p;
10087
return this;
10188
}
102-
89+
90+
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
10391
@Override
10492
public JsonParser getProcessor() {
105-
return _processor;
93+
return super.getProcessor();
10694
}
10795

108-
/**
109-
* Method that may be called to find payload that was being parsed, if
110-
* one was specified for parser that threw this Exception.
111-
*
112-
* @return request body, if payload was specified; `null` otherwise
113-
*
114-
* @since 2.8
115-
*/
96+
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
97+
@Override
11698
public RequestPayload getRequestPayload() {
117-
return _requestPayload;
99+
return super.getRequestPayload();
118100
}
119101

120-
/**
121-
* The method returns the String representation of the request payload if
122-
* one was specified for parser that threw this Exception.
123-
*
124-
* @return request body as String, if payload was specified; `null` otherwise
125-
*
126-
* @since 2.8
127-
*/
102+
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
103+
@Override
128104
public String getRequestPayloadAsString() {
129-
return (_requestPayload != null) ? _requestPayload.toString() : null;
105+
return super.getRequestPayloadAsString();
130106
}
131107

132-
/**
133-
* Overriding the getMessage() to include the request body
134-
*/
108+
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
135109
@Override
136110
public String getMessage() {
137-
String msg = super.getMessage();
138-
if (_requestPayload != null) {
139-
msg += "\nRequest payload : " + _requestPayload.toString();
140-
}
141-
return msg;
111+
return super.getMessage();
142112
}
143113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fasterxml.jackson.core.exc;
2+
3+
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.util.RequestPayload;
5+
6+
/**
7+
* Exception type for read-side problems that are not direct decoding ("parsing")
8+
* problems (those would be reported as {@link com.fasterxml.jackson.core.JsonParseException}s),
9+
* but rather result from failed attempts to convert specific Java value out of valid
10+
* but incompatible input value. One example is numeric coercions where target number type's
11+
* range does not allow mapping of too large/too small input value.
12+
*
13+
* @since 2.10
14+
*/
15+
public class InputCoercionException extends JsonReadException {
16+
private static final long serialVersionUID = 1L;
17+
18+
/**
19+
* Input token that represents input value that failed to coerce.
20+
*/
21+
protected final JsonToken _inputType;
22+
23+
/**
24+
* Target type that input value failed to coerce to.
25+
*/
26+
protected final Class<?> _targetType;
27+
28+
/**
29+
* Constructor that uses current parsing location as location, and
30+
* sets processor (accessible via {@link #getProcessor()}) to
31+
* specified parser.
32+
*/
33+
public InputCoercionException(JsonParser p, JsonToken inputType, Class<?> targetType,
34+
String msg) {
35+
super(p, msg);
36+
_inputType = inputType;
37+
_targetType = targetType;
38+
}
39+
40+
/**
41+
* Fluent method that may be used to assign originating {@link JsonParser},
42+
* to be accessed using {@link #getProcessor()}.
43+
*<p>
44+
* NOTE: `this` instance is modified and no new instance is constructed.
45+
*/
46+
@Override
47+
public InputCoercionException withParser(JsonParser p) {
48+
_processor = p;
49+
return this;
50+
}
51+
52+
@Override
53+
public InputCoercionException withRequestPayload(RequestPayload p) {
54+
_requestPayload = p;
55+
return this;
56+
}
57+
58+
/**
59+
* Accessor for getting information about input type (in form of token, giving "shape"
60+
* of input) for which coercion failed.
61+
*/
62+
public JsonToken getInputType() {
63+
return _inputType;
64+
}
65+
66+
/**
67+
* Accessor for getting information about target type (in form of Java {@link java.lang.Class})
68+
* for which coercion failed.
69+
*/
70+
public Class<?> getTargetType() {
71+
return _targetType;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.fasterxml.jackson.core.exc;
2+
3+
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.util.RequestPayload;
5+
6+
/**
7+
* Intermediate base class for all read-side streaming processing problems, including
8+
* parsing and input value coercion problems.
9+
*
10+
* @since 2.10
11+
*/
12+
public abstract class JsonReadException
13+
extends JsonProcessingException
14+
{
15+
final static long serialVersionUID = 1L;
16+
17+
protected transient JsonParser _processor;
18+
19+
/**
20+
* Optional payload that can be assigned to pass along for error reporting
21+
* or handling purposes. Core streaming parser implementations DO NOT
22+
* initialize this; it is up to using applications and frameworks to
23+
* populate it.
24+
*/
25+
protected RequestPayload _requestPayload;
26+
27+
public JsonReadException(JsonParser p, String msg) {
28+
super(msg, (p == null) ? null : p.getCurrentLocation());
29+
_processor = p;
30+
}
31+
32+
public JsonReadException(JsonParser p, String msg, Throwable root) {
33+
super(msg, (p == null) ? null : p.getCurrentLocation(), root);
34+
_processor = p;
35+
}
36+
37+
public JsonReadException(JsonParser p, String msg, JsonLocation loc) {
38+
super(msg, loc, null);
39+
_processor = p;
40+
}
41+
42+
protected JsonReadException(String msg, JsonLocation loc, Throwable rootCause) {
43+
super(msg);
44+
if (rootCause != null) {
45+
initCause(rootCause);
46+
}
47+
_location = loc;
48+
}
49+
50+
/**
51+
* Fluent method that may be used to assign originating {@link JsonParser},
52+
* to be accessed using {@link #getProcessor()}.
53+
*<p>
54+
* NOTE: `this` instance is modified and no new instance is constructed.
55+
*/
56+
public abstract JsonReadException withParser(JsonParser p);
57+
58+
/**
59+
* Fluent method that may be used to assign payload to this exception,
60+
* to let recipient access it for diagnostics purposes.
61+
*<p>
62+
* NOTE: `this` instance is modified and no new instance is constructed.
63+
*/
64+
public abstract JsonReadException withRequestPayload(RequestPayload p);
65+
66+
@Override
67+
public JsonParser getProcessor() {
68+
return _processor;
69+
}
70+
71+
/**
72+
* Method that may be called to find payload that was being parsed, if
73+
* one was specified for parser that threw this Exception.
74+
*
75+
* @return request body, if payload was specified; `null` otherwise
76+
*/
77+
public RequestPayload getRequestPayload() {
78+
return _requestPayload;
79+
}
80+
81+
/**
82+
* The method returns the String representation of the request payload if
83+
* one was specified for parser that threw this Exception.
84+
*
85+
* @return request body as String, if payload was specified; `null` otherwise
86+
*/
87+
public String getRequestPayloadAsString() {
88+
return (_requestPayload != null) ? _requestPayload.toString() : null;
89+
}
90+
91+
/**
92+
* Overriding the getMessage() to include the request body
93+
*/
94+
@Override
95+
public String getMessage() {
96+
String msg = super.getMessage();
97+
if (_requestPayload != null) {
98+
msg += "\nRequest payload : " + _requestPayload.toString();
99+
}
100+
return msg;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Package for some of {@link com.fasterxml.jackson.core.JsonProcessingException}
3+
* subtypes contained by streaming API.
4+
*/
5+
package com.fasterxml.jackson.core.exc;

0 commit comments

Comments
 (0)