Tatu Saloranta | 68bb83d | 2013-04-19 10:41:15 -0700 | [diff] [blame] | 1 | /* Jackson JSON-processor. |
| 2 | * |
| 3 | * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi |
| 4 | */ |
| 5 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 6 | package com.fasterxml.jackson.core; |
| 7 | |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 8 | import com.fasterxml.jackson.core.util.RequestPayload; |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 9 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 10 | /** |
| 11 | * Exception type for parsing problems, used when non-well-formed content |
| 12 | * (content that does not conform to JSON syntax as per specification) |
| 13 | * is encountered. |
| 14 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 15 | public class JsonParseException extends JsonProcessingException { |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 16 | private static final long serialVersionUID = 2L; // 2.7 |
| 17 | |
Tatu Saloranta | 46fbbd3 | 2016-04-14 18:45:50 -0700 | [diff] [blame] | 18 | // transient since 2.7.4 |
Tatu Saloranta | d652a84 | 2016-04-14 18:43:55 -0700 | [diff] [blame] | 19 | protected transient JsonParser _processor; |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 20 | |
Tatu Saloranta | 46fbbd3 | 2016-04-14 18:45:50 -0700 | [diff] [blame] | 21 | /** |
Tatu Saloranta | 51e1f98 | 2016-05-06 17:39:29 -0700 | [diff] [blame] | 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 | * |
Tatu Saloranta | 46fbbd3 | 2016-04-14 18:45:50 -0700 | [diff] [blame] | 27 | * @since 2.8 |
| 28 | */ |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 29 | protected RequestPayload _requestPayload; |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 30 | |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 31 | @Deprecated // since 2.7 |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 32 | public JsonParseException(String msg, JsonLocation loc) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 33 | super(msg, loc); |
| 34 | } |
| 35 | |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 36 | @Deprecated // since 2.7 |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 37 | public JsonParseException(String msg, JsonLocation loc, Throwable root) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 38 | super(msg, loc, root); |
| 39 | } |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Constructor that uses current parsing location as location, and |
| 43 | * sets processor (accessible via {@link #getProcessor()}) to |
| 44 | * specified parser. |
| 45 | * |
| 46 | * @since 2.7 |
| 47 | */ |
| 48 | public JsonParseException(JsonParser p, String msg) { |
| 49 | super(msg, (p == null) ? null : p.getCurrentLocation()); |
| 50 | _processor = p; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @since 2.7 |
| 55 | */ |
| 56 | public JsonParseException(JsonParser p, String msg, Throwable root) { |
| 57 | super(msg, (p == null) ? null : p.getCurrentLocation(), root); |
| 58 | _processor = p; |
| 59 | } |
Tatu Saloranta | 7ff1ff8 | 2017-05-23 15:25:43 -0700 | [diff] [blame] | 60 | |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 61 | /** |
| 62 | * @since 2.7 |
| 63 | */ |
| 64 | public JsonParseException(JsonParser p, String msg, JsonLocation loc) { |
| 65 | super(msg, loc); |
| 66 | _processor = p; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @since 2.7 |
| 71 | */ |
| 72 | public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root) { |
| 73 | super(msg, loc, root); |
| 74 | _processor = p; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Fluent method that may be used to assign originating {@link JsonParser}, |
| 79 | * to be accessed using {@link #getProcessor()}. |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 80 | *<p> |
| 81 | * NOTE: `this` instance is modified and no new instance is constructed. |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 82 | * |
| 83 | * @since 2.7 |
| 84 | */ |
| 85 | public JsonParseException withParser(JsonParser p) { |
| 86 | _processor = p; |
| 87 | return this; |
| 88 | } |
| 89 | |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Fluent method that may be used to assign payload to this exception, |
| 92 | * to let recipient access it for diagnostics purposes. |
| 93 | *<p> |
| 94 | * NOTE: `this` instance is modified and no new instance is constructed. |
| 95 | * |
| 96 | * @since 2.8 |
| 97 | */ |
| 98 | public JsonParseException withRequestPayload(RequestPayload p) { |
| 99 | _requestPayload = p; |
| 100 | return this; |
| 101 | } |
| 102 | |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public JsonParser getProcessor() { |
| 105 | return _processor; |
| 106 | } |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 107 | |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame] | 108 | /** |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 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 |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame] | 115 | */ |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 116 | public RequestPayload getRequestPayload() { |
| 117 | return _requestPayload; |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame] | 118 | } |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 119 | |
| 120 | /** |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 121 | * The method returns the String representation of the request payload if |
| 122 | * one was specified for parser that threw this Exception. |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame] | 123 | * |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 124 | * @return request body as String, if payload was specified; `null` otherwise |
| 125 | * |
| 126 | * @since 2.8 |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 127 | */ |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 128 | public String getRequestPayloadAsString() { |
| 129 | return (_requestPayload != null) ? _requestPayload.toString() : null; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Overriding the getMessage() to include the request body |
| 134 | */ |
| 135 | @Override |
| 136 | public String getMessage() { |
Tatu Saloranta | 6e3999e | 2016-04-14 18:42:19 -0700 | [diff] [blame] | 137 | String msg = super.getMessage(); |
| 138 | if (_requestPayload != null) { |
| 139 | msg += "\nRequest payload : " + _requestPayload.toString(); |
| 140 | } |
| 141 | return msg; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 142 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 143 | } |