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 | |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 8 | import java.io.ByteArrayOutputStream; |
| 9 | import java.io.IOException; |
| 10 | import java.io.InputStream; |
| 11 | import java.io.Reader; |
| 12 | import java.io.StringWriter; |
| 13 | import java.nio.CharBuffer; |
| 14 | import java.nio.charset.Charset; |
| 15 | |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 16 | import com.fasterxml.jackson.core.util.RequestPayloadWrapper; |
| 17 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 18 | /** |
| 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 Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 23 | public class JsonParseException extends JsonProcessingException { |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 24 | private static final long serialVersionUID = 2L; // 2.7 |
| 25 | |
| 26 | protected JsonParser _processor; |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 27 | protected RequestPayloadWrapper requestPayload; |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 28 | |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 29 | @Deprecated // since 2.7 |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 30 | public JsonParseException(String msg, JsonLocation loc) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 31 | super(msg, loc); |
| 32 | } |
| 33 | |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 34 | @Deprecated // since 2.7 |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 35 | public JsonParseException(String msg, JsonLocation loc, Throwable root) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 36 | super(msg, loc, root); |
| 37 | } |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 38 | |
| 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 | } |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | ******************************************************************* |
| 77 | Extended Constructors for setting the Request Body in the exception |
| 78 | ******************************************************************* |
| 79 | */ |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 80 | public JsonParseException(JsonParser p, String msg, RequestPayloadWrapper requestPayload) { |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 81 | this(p, msg); |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 82 | this.requestPayload = requestPayload; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 83 | } |
| 84 | |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 85 | public JsonParseException(JsonParser p, String msg, Throwable root, RequestPayloadWrapper requestPayload) { |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 86 | this(p, msg, root); |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 87 | this.requestPayload = requestPayload; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 90 | public JsonParseException(JsonParser p, String msg, JsonLocation loc, RequestPayloadWrapper requestPayload) { |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 91 | this(p, msg, loc); |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 92 | this.requestPayload = requestPayload; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 93 | } |
| 94 | |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 95 | public JsonParseException(JsonParser p, String msg, JsonLocation loc, Throwable root, RequestPayloadWrapper requestPayload) { |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 96 | this(p, msg, loc, root); |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 97 | this.requestPayload = requestPayload; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 98 | } |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 99 | |
| 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 | } |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame^] | 115 | |
| 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 | } |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 125 | |
| 126 | /** |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame^] | 127 | * 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 |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 132 | */ |
LokeshN | 0a5291f | 2016-04-11 20:47:33 +0530 | [diff] [blame^] | 133 | public String getRequestPayload() { |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 134 | return requestPayload != null ? requestPayload.toString() : null; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Overriding the getMessage() to include the request body |
| 139 | */ |
| 140 | @Override |
| 141 | public String getMessage() { |
| 142 | String msg = super.getMessage(); |
Lokesh N | 42aeabf | 2016-03-22 20:36:53 +0530 | [diff] [blame] | 143 | return requestPayload != null ? (msg + "\nRequest Payload : " + getRequestPayload()) : msg; |
LokeshN | 27001ed | 2016-03-15 15:47:26 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 147 | } |