Skip to content

Commit

Permalink
Start work on #508, new exception type
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 16, 2019
1 parent 04bba39 commit e491c28
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 50 deletions.
70 changes: 20 additions & 50 deletions src/main/java/com/fasterxml/jackson/core/JsonParseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@

package com.fasterxml.jackson.core;

import com.fasterxml.jackson.core.exc.JsonReadException;
import com.fasterxml.jackson.core.util.RequestPayload;

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

// transient since 2.7.4
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.
*
* @since 2.8
*/
protected RequestPayload _requestPayload;

@Deprecated // since 2.7
public JsonParseException(String msg, JsonLocation loc) {
super(msg, loc);
super(msg, loc, null);
}

@Deprecated // since 2.7
Expand All @@ -46,32 +35,28 @@ public JsonParseException(String msg, JsonLocation loc, Throwable root) {
* @since 2.7
*/
public JsonParseException(JsonParser p, String msg) {
super(msg, (p == null) ? null : p.getCurrentLocation());
_processor = p;
super(p, msg);
}

/**
* @since 2.7
*/
public JsonParseException(JsonParser p, String msg, Throwable root) {
super(msg, (p == null) ? null : p.getCurrentLocation(), root);
_processor = p;
super(p, msg, root);
}

/**
* @since 2.7
*/
public JsonParseException(JsonParser p, String msg, JsonLocation loc) {
super(msg, loc);
_processor = p;
super(p, msg, loc);
}

/**
* @since 2.7
*/
public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root) {
super(msg, loc, root);
_processor = p;
}

/**
Expand All @@ -82,6 +67,7 @@ public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable
*
* @since 2.7
*/
@Override
public JsonParseException withParser(JsonParser p) {
_processor = p;
return this;
Expand All @@ -95,49 +81,33 @@ public JsonParseException withParser(JsonParser p) {
*
* @since 2.8
*/
@Override
public JsonParseException withRequestPayload(RequestPayload p) {
_requestPayload = p;
return this;
}


// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
@Override
public JsonParser getProcessor() {
return _processor;
return super.getProcessor();
}

/**
* 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
*
* @since 2.8
*/
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
@Override
public RequestPayload getRequestPayload() {
return _requestPayload;
return super.getRequestPayload();
}

/**
* 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
*
* @since 2.8
*/
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
@Override
public String getRequestPayloadAsString() {
return (_requestPayload != null) ? _requestPayload.toString() : null;
return super.getRequestPayloadAsString();
}

/**
* Overriding the getMessage() to include the request body
*/
// NOTE: overloaded in 2.10 just to retain binary compatibility with 2.9 (remove from 3.0)
@Override
public String getMessage() {
String msg = super.getMessage();
if (_requestPayload != null) {
msg += "\nRequest payload : " + _requestPayload.toString();
}
return msg;
return super.getMessage();
}
}
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 src/main/java/com/fasterxml/jackson/core/exc/JsonReadException.java
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;
}
}
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;

0 comments on commit e491c28

Please sign in to comment.