blob: c9662dd04a08948478b24f6a4f3baab2b3ec58ea [file] [log] [blame]
Tatu Saloranta68bb83d2013-04-19 10:41:15 -07001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4 */
5
Tatu Salorantaf15531c2011-12-22 23:00:40 -08006package com.fasterxml.jackson.core;
7
Tatu Saloranta6e3999e2016-04-14 18:42:19 -07008import com.fasterxml.jackson.core.util.RequestPayload;
Lokesh N42aeabf2016-03-22 20:36:53 +05309
Tatu Salorantaf15531c2011-12-22 23:00:40 -080010/**
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 Saloranta32e4e912014-01-26 19:59:28 -080015public class JsonParseException extends JsonProcessingException {
Tatu Saloranta77be53c2015-08-17 22:52:56 -070016 private static final long serialVersionUID = 2L; // 2.7
17
Tatu Saloranta46fbbd32016-04-14 18:45:50 -070018 // transient since 2.7.4
Tatu Salorantad652a842016-04-14 18:43:55 -070019 protected transient JsonParser _processor;
Tatu Saloranta77be53c2015-08-17 22:52:56 -070020
Tatu Saloranta46fbbd32016-04-14 18:45:50 -070021 /**
Tatu Saloranta51e1f982016-05-06 17:39:29 -070022 * 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 Saloranta46fbbd32016-04-14 18:45:50 -070027 * @since 2.8
28 */
Tatu Saloranta6e3999e2016-04-14 18:42:19 -070029 protected RequestPayload _requestPayload;
Tatu Saloranta77be53c2015-08-17 22:52:56 -070030
Tatu Salorantad302ae02015-12-27 21:53:15 -080031 @Deprecated // since 2.7
Tatu Saloranta32e4e912014-01-26 19:59:28 -080032 public JsonParseException(String msg, JsonLocation loc) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080033 super(msg, loc);
34 }
35
Tatu Salorantad302ae02015-12-27 21:53:15 -080036 @Deprecated // since 2.7
Tatu Saloranta32e4e912014-01-26 19:59:28 -080037 public JsonParseException(String msg, JsonLocation loc, Throwable root) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080038 super(msg, loc, root);
39 }
Tatu Saloranta77be53c2015-08-17 22:52:56 -070040
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 Saloranta7ff1ff82017-05-23 15:25:43 -070060
Tatu Saloranta77be53c2015-08-17 22:52:56 -070061 /**
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 Saloranta6e3999e2016-04-14 18:42:19 -070080 *<p>
81 * NOTE: `this` instance is modified and no new instance is constructed.
Tatu Saloranta77be53c2015-08-17 22:52:56 -070082 *
83 * @since 2.7
84 */
85 public JsonParseException withParser(JsonParser p) {
86 _processor = p;
87 return this;
88 }
89
Tatu Saloranta6e3999e2016-04-14 18:42:19 -070090 /**
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 Saloranta77be53c2015-08-17 22:52:56 -0700103 @Override
104 public JsonParser getProcessor() {
105 return _processor;
106 }
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700107
LokeshN0a5291f2016-04-11 20:47:33 +0530108 /**
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700109 * 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
LokeshN0a5291f2016-04-11 20:47:33 +0530115 */
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700116 public RequestPayload getRequestPayload() {
117 return _requestPayload;
LokeshN0a5291f2016-04-11 20:47:33 +0530118 }
LokeshN27001ed2016-03-15 15:47:26 +0530119
120 /**
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700121 * The method returns the String representation of the request payload if
122 * one was specified for parser that threw this Exception.
LokeshN0a5291f2016-04-11 20:47:33 +0530123 *
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700124 * @return request body as String, if payload was specified; `null` otherwise
125 *
126 * @since 2.8
LokeshN27001ed2016-03-15 15:47:26 +0530127 */
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700128 public String getRequestPayloadAsString() {
129 return (_requestPayload != null) ? _requestPayload.toString() : null;
LokeshN27001ed2016-03-15 15:47:26 +0530130 }
131
132 /**
133 * Overriding the getMessage() to include the request body
134 */
135 @Override
136 public String getMessage() {
Tatu Saloranta6e3999e2016-04-14 18:42:19 -0700137 String msg = super.getMessage();
138 if (_requestPayload != null) {
139 msg += "\nRequest payload : " + _requestPayload.toString();
140 }
141 return msg;
LokeshN27001ed2016-03-15 15:47:26 +0530142 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800143}