-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start work on #508, new exception type
- Loading branch information
1 parent
04bba39
commit e491c28
Showing
4 changed files
with
200 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/fasterxml/jackson/core/exc/InputCoercionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.fasterxml.jackson.core.exc; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
import com.fasterxml.jackson.core.util.RequestPayload; | ||
|
||
/** | ||
* Exception type for read-side problems that are not direct decoding ("parsing") | ||
* problems (those would be reported as {@link com.fasterxml.jackson.core.JsonParseException}s), | ||
* but rather result from failed attempts to convert specific Java value out of valid | ||
* but incompatible input value. One example is numeric coercions where target number type's | ||
* range does not allow mapping of too large/too small input value. | ||
* | ||
* @since 2.10 | ||
*/ | ||
public class InputCoercionException extends JsonReadException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Input token that represents input value that failed to coerce. | ||
*/ | ||
protected final JsonToken _inputType; | ||
|
||
/** | ||
* Target type that input value failed to coerce to. | ||
*/ | ||
protected final Class<?> _targetType; | ||
|
||
/** | ||
* Constructor that uses current parsing location as location, and | ||
* sets processor (accessible via {@link #getProcessor()}) to | ||
* specified parser. | ||
*/ | ||
public InputCoercionException(JsonParser p, JsonToken inputType, Class<?> targetType, | ||
String msg) { | ||
super(p, msg); | ||
_inputType = inputType; | ||
_targetType = targetType; | ||
} | ||
|
||
/** | ||
* Fluent method that may be used to assign originating {@link JsonParser}, | ||
* to be accessed using {@link #getProcessor()}. | ||
*<p> | ||
* NOTE: `this` instance is modified and no new instance is constructed. | ||
*/ | ||
@Override | ||
public InputCoercionException withParser(JsonParser p) { | ||
_processor = p; | ||
return this; | ||
} | ||
|
||
@Override | ||
public InputCoercionException withRequestPayload(RequestPayload p) { | ||
_requestPayload = p; | ||
return this; | ||
} | ||
|
||
/** | ||
* Accessor for getting information about input type (in form of token, giving "shape" | ||
* of input) for which coercion failed. | ||
*/ | ||
public JsonToken getInputType() { | ||
return _inputType; | ||
} | ||
|
||
/** | ||
* Accessor for getting information about target type (in form of Java {@link java.lang.Class}) | ||
* for which coercion failed. | ||
*/ | ||
public Class<?> getTargetType() { | ||
return _targetType; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/com/fasterxml/jackson/core/exc/JsonReadException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.fasterxml.jackson.core.exc; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
import com.fasterxml.jackson.core.util.RequestPayload; | ||
|
||
/** | ||
* Intermediate base class for all read-side streaming processing problems, including | ||
* parsing and input value coercion problems. | ||
* | ||
* @since 2.10 | ||
*/ | ||
public abstract class JsonReadException | ||
extends JsonProcessingException | ||
{ | ||
final static long serialVersionUID = 1L; | ||
|
||
protected transient JsonParser _processor; | ||
|
||
/** | ||
* Optional payload that can be assigned to pass along for error reporting | ||
* or handling purposes. Core streaming parser implementations DO NOT | ||
* initialize this; it is up to using applications and frameworks to | ||
* populate it. | ||
*/ | ||
protected RequestPayload _requestPayload; | ||
|
||
public JsonReadException(JsonParser p, String msg) { | ||
super(msg, (p == null) ? null : p.getCurrentLocation()); | ||
_processor = p; | ||
} | ||
|
||
public JsonReadException(JsonParser p, String msg, Throwable root) { | ||
super(msg, (p == null) ? null : p.getCurrentLocation(), root); | ||
_processor = p; | ||
} | ||
|
||
public JsonReadException(JsonParser p, String msg, JsonLocation loc) { | ||
super(msg, loc, null); | ||
_processor = p; | ||
} | ||
|
||
protected JsonReadException(String msg, JsonLocation loc, Throwable rootCause) { | ||
super(msg); | ||
if (rootCause != null) { | ||
initCause(rootCause); | ||
} | ||
_location = loc; | ||
} | ||
|
||
/** | ||
* Fluent method that may be used to assign originating {@link JsonParser}, | ||
* to be accessed using {@link #getProcessor()}. | ||
*<p> | ||
* NOTE: `this` instance is modified and no new instance is constructed. | ||
*/ | ||
public abstract JsonReadException withParser(JsonParser p); | ||
|
||
/** | ||
* Fluent method that may be used to assign payload to this exception, | ||
* to let recipient access it for diagnostics purposes. | ||
*<p> | ||
* NOTE: `this` instance is modified and no new instance is constructed. | ||
*/ | ||
public abstract JsonReadException withRequestPayload(RequestPayload p); | ||
|
||
@Override | ||
public JsonParser getProcessor() { | ||
return _processor; | ||
} | ||
|
||
/** | ||
* Method that may be called to find payload that was being parsed, if | ||
* one was specified for parser that threw this Exception. | ||
* | ||
* @return request body, if payload was specified; `null` otherwise | ||
*/ | ||
public RequestPayload getRequestPayload() { | ||
return _requestPayload; | ||
} | ||
|
||
/** | ||
* The method returns the String representation of the request payload if | ||
* one was specified for parser that threw this Exception. | ||
* | ||
* @return request body as String, if payload was specified; `null` otherwise | ||
*/ | ||
public String getRequestPayloadAsString() { | ||
return (_requestPayload != null) ? _requestPayload.toString() : null; | ||
} | ||
|
||
/** | ||
* Overriding the getMessage() to include the request body | ||
*/ | ||
@Override | ||
public String getMessage() { | ||
String msg = super.getMessage(); | ||
if (_requestPayload != null) { | ||
msg += "\nRequest payload : " + _requestPayload.toString(); | ||
} | ||
return msg; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/fasterxml/jackson/core/exc/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Package for some of {@link com.fasterxml.jackson.core.JsonProcessingException} | ||
* subtypes contained by streaming API. | ||
*/ | ||
package com.fasterxml.jackson.core.exc; |