From 07b45ef7c07ad55098b4ce4f2be3e5690a46c9af Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 27 Dec 2023 20:22:43 -0800 Subject: [PATCH] Refactor exception generation by JsonParser (#1179) --- .../jackson/core/JsonParseException.java | 35 ++++++++++--------- .../jackson/core/base/ParserMinimalBase.java | 2 +- .../jackson/core/exc/StreamReadException.java | 35 ++++++++++--------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonParseException.java b/src/main/java/com/fasterxml/jackson/core/JsonParseException.java index a5eb101dcd..a84f83d89c 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonParseException.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonParseException.java @@ -18,14 +18,9 @@ public class JsonParseException { private static final long serialVersionUID = 2L; // 2.7 - @Deprecated // since 2.7 - public JsonParseException(String msg, JsonLocation loc) { - super(msg, loc, null); - } - - @Deprecated // since 2.7 - public JsonParseException(String msg, JsonLocation loc, Throwable root) { - super(msg, loc, root); + // @since 2.15 + public JsonParseException(String msg) { + this(null, msg, null, null); } /** @@ -39,27 +34,33 @@ public JsonParseException(String msg, JsonLocation loc, Throwable root) { * @since 2.7 */ public JsonParseException(JsonParser p, String msg) { - super(p, msg); + this(p, msg, _currentLocation(p), null); } // @since 2.7 - public JsonParseException(JsonParser p, String msg, Throwable root) { - super(p, msg, root); + public JsonParseException(JsonParser p, String msg, Throwable rootCause) { + this(p, msg, _currentLocation(p), rootCause); } // @since 2.7 public JsonParseException(JsonParser p, String msg, JsonLocation loc) { - super(p, msg, loc); + this(p, msg, loc, null); } + // Canonical constructor // @since 2.7 - public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root) { - super(p, msg, loc, root); + public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable rootCause) { + super(p, msg, loc, rootCause); } - // @since 2.15 - public JsonParseException(String msg) { - super(msg); + @Deprecated // since 2.7 + public JsonParseException(String msg, JsonLocation loc) { + this(null, msg, loc, null); + } + + @Deprecated // since 2.7 + public JsonParseException(String msg, JsonLocation loc, Throwable rootCause) { + this(null, msg, loc, rootCause); } /** diff --git a/src/main/java/com/fasterxml/jackson/core/base/ParserMinimalBase.java b/src/main/java/com/fasterxml/jackson/core/base/ParserMinimalBase.java index ef69ba9508..bf88f36c7b 100644 --- a/src/main/java/com/fasterxml/jackson/core/base/ParserMinimalBase.java +++ b/src/main/java/com/fasterxml/jackson/core/base/ParserMinimalBase.java @@ -774,7 +774,7 @@ protected final void _throwInternal() { } protected final JsonParseException _constructError(String msg, Throwable t) { - return new JsonParseException(this, msg, t); + return new JsonParseException(this, msg, currentLocation(), t); } @Deprecated // since 2.11 diff --git a/src/main/java/com/fasterxml/jackson/core/exc/StreamReadException.java b/src/main/java/com/fasterxml/jackson/core/exc/StreamReadException.java index 1754be6534..ee5591b00a 100644 --- a/src/main/java/com/fasterxml/jackson/core/exc/StreamReadException.java +++ b/src/main/java/com/fasterxml/jackson/core/exc/StreamReadException.java @@ -26,21 +26,28 @@ public abstract class StreamReadException */ protected RequestPayload _requestPayload; + // @since 2.15 + protected StreamReadException(String msg) { + this(null, msg, null, null); + } + protected StreamReadException(JsonParser p, String msg) { - super(msg, (p == null) ? null : p.currentLocation()); - _processor = p; + this(p, msg, _currentLocation(p), null); } - protected StreamReadException(JsonParser p, String msg, Throwable root) { - super(msg, (p == null) ? null : p.currentLocation(), root); - _processor = p; + protected StreamReadException(JsonParser p, String msg, Throwable rootCause) { + this(p, msg, _currentLocation(p), rootCause); } protected StreamReadException(JsonParser p, String msg, JsonLocation loc) { - super(msg, loc, null); - _processor = p; + this(p, msg, loc, null); + } + + protected StreamReadException(String msg, JsonLocation loc, Throwable rootCause) { + this(null, msg, loc, rootCause); } + // Canonical constructor // @since 2.13 protected StreamReadException(JsonParser p, String msg, JsonLocation loc, Throwable rootCause) { @@ -48,15 +55,6 @@ protected StreamReadException(JsonParser p, String msg, JsonLocation loc, _processor = p; } - protected StreamReadException(String msg, JsonLocation loc, Throwable rootCause) { - super(msg, loc, rootCause); - } - - // @since 2.15 - protected StreamReadException(String msg) { - super(msg); - } - /** * Fluent method that may be used to assign originating {@link JsonParser}, * to be accessed using {@link #getProcessor()}. @@ -117,4 +115,9 @@ public String getMessage() { } return msg; } + + // @since 2.17 + protected static JsonLocation _currentLocation(JsonParser p) { + return (p == null) ? null : p.currentLocation(); + } }