blob: 96e425b796fb54f52affcb59760240b9d1299dcb [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
LokeshN27001ed2016-03-15 15:47:26 +05308import java.io.ByteArrayOutputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.Reader;
12import java.io.StringWriter;
13import java.nio.CharBuffer;
14import java.nio.charset.Charset;
15
Lokesh N42aeabf2016-03-22 20:36:53 +053016import com.fasterxml.jackson.core.util.RequestPayloadWrapper;
17
Tatu Salorantaf15531c2011-12-22 23:00:40 -080018/**
19 * Exception type for parsing problems, used when non-well-formed content
20 * (content that does not conform to JSON syntax as per specification)
21 * is encountered.
22 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -080023public class JsonParseException extends JsonProcessingException {
Tatu Saloranta77be53c2015-08-17 22:52:56 -070024 private static final long serialVersionUID = 2L; // 2.7
25
26 protected JsonParser _processor;
Lokesh N42aeabf2016-03-22 20:36:53 +053027 protected RequestPayloadWrapper requestPayload;
Tatu Saloranta77be53c2015-08-17 22:52:56 -070028
Tatu Salorantad302ae02015-12-27 21:53:15 -080029 @Deprecated // since 2.7
Tatu Saloranta32e4e912014-01-26 19:59:28 -080030 public JsonParseException(String msg, JsonLocation loc) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080031 super(msg, loc);
32 }
33
Tatu Salorantad302ae02015-12-27 21:53:15 -080034 @Deprecated // since 2.7
Tatu Saloranta32e4e912014-01-26 19:59:28 -080035 public JsonParseException(String msg, JsonLocation loc, Throwable root) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080036 super(msg, loc, root);
37 }
Tatu Saloranta77be53c2015-08-17 22:52:56 -070038
39 /**
40 * Constructor that uses current parsing location as location, and
41 * sets processor (accessible via {@link #getProcessor()}) to
42 * specified parser.
43 *
44 * @since 2.7
45 */
46 public JsonParseException(JsonParser p, String msg) {
47 super(msg, (p == null) ? null : p.getCurrentLocation());
48 _processor = p;
49 }
50
51 /**
52 * @since 2.7
53 */
54 public JsonParseException(JsonParser p, String msg, Throwable root) {
55 super(msg, (p == null) ? null : p.getCurrentLocation(), root);
56 _processor = p;
57 }
58
59 /**
60 * @since 2.7
61 */
62 public JsonParseException(JsonParser p, String msg, JsonLocation loc) {
63 super(msg, loc);
64 _processor = p;
65 }
66
67 /**
68 * @since 2.7
69 */
70 public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root) {
71 super(msg, loc, root);
72 _processor = p;
73 }
LokeshN27001ed2016-03-15 15:47:26 +053074
75 /*
76 *******************************************************************
77 Extended Constructors for setting the Request Body in the exception
78 *******************************************************************
79 */
Lokesh N42aeabf2016-03-22 20:36:53 +053080 public JsonParseException(JsonParser p, String msg, RequestPayloadWrapper requestPayload) {
LokeshN27001ed2016-03-15 15:47:26 +053081 this(p, msg);
Lokesh N42aeabf2016-03-22 20:36:53 +053082 this.requestPayload = requestPayload;
LokeshN27001ed2016-03-15 15:47:26 +053083 }
84
Lokesh N42aeabf2016-03-22 20:36:53 +053085 public JsonParseException(JsonParser p, String msg, Throwable root, RequestPayloadWrapper requestPayload) {
LokeshN27001ed2016-03-15 15:47:26 +053086 this(p, msg, root);
Lokesh N42aeabf2016-03-22 20:36:53 +053087 this.requestPayload = requestPayload;
LokeshN27001ed2016-03-15 15:47:26 +053088 }
89
Lokesh N42aeabf2016-03-22 20:36:53 +053090 public JsonParseException(JsonParser p, String msg, JsonLocation loc, RequestPayloadWrapper requestPayload) {
LokeshN27001ed2016-03-15 15:47:26 +053091 this(p, msg, loc);
Lokesh N42aeabf2016-03-22 20:36:53 +053092 this.requestPayload = requestPayload;
LokeshN27001ed2016-03-15 15:47:26 +053093 }
94
Lokesh N42aeabf2016-03-22 20:36:53 +053095 public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root, RequestPayloadWrapper requestPayload) {
LokeshN27001ed2016-03-15 15:47:26 +053096 this(p, msg, loc, root);
Lokesh N42aeabf2016-03-22 20:36:53 +053097 this.requestPayload = requestPayload;
LokeshN27001ed2016-03-15 15:47:26 +053098 }
Tatu Saloranta77be53c2015-08-17 22:52:56 -070099
100 /**
101 * Fluent method that may be used to assign originating {@link JsonParser},
102 * to be accessed using {@link #getProcessor()}.
103 *
104 * @since 2.7
105 */
106 public JsonParseException withParser(JsonParser p) {
107 _processor = p;
108 return this;
109 }
110
111 @Override
112 public JsonParser getProcessor() {
113 return _processor;
114 }
LokeshN0a5291f2016-04-11 20:47:33 +0530115
116 /**
117 * Method to get the raw request payload
118 * The raw payload will be in either byte[] or String
119 *
120 * @return raw request payload object either in bytes or in string
121 */
122 public Object getRawRequestPayload() {
123 return requestPayload != null ? requestPayload.getRawRequestPayload() : null;
124 }
LokeshN27001ed2016-03-15 15:47:26 +0530125
126 /**
LokeshN0a5291f2016-04-11 20:47:33 +0530127 * The method returns the String representation of the request payload
128 * The raw request payload could be in String or in byte[], the method
129 * returns the raw request payload in String format
130 *
131 * @return request body as String
LokeshN27001ed2016-03-15 15:47:26 +0530132 */
LokeshN0a5291f2016-04-11 20:47:33 +0530133 public String getRequestPayload() {
Lokesh N42aeabf2016-03-22 20:36:53 +0530134 return requestPayload != null ? requestPayload.toString() : null;
LokeshN27001ed2016-03-15 15:47:26 +0530135 }
136
137 /**
138 * Overriding the getMessage() to include the request body
139 */
140 @Override
141 public String getMessage() {
142 String msg = super.getMessage();
Lokesh N42aeabf2016-03-22 20:36:53 +0530143 return requestPayload != null ? (msg + "\nRequest Payload : " + getRequestPayload()) : msg;
LokeshN27001ed2016-03-15 15:47:26 +0530144 }
145
146
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800147}