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 | |
| 8 | /** |
| 9 | * Intermediate base class for all problems encountered when |
| 10 | * processing (parsing, generating) JSON content |
| 11 | * that are not pure I/O problems. |
| 12 | * Regular {@link java.io.IOException}s will be passed through as is. |
| 13 | * Sub-class of {@link java.io.IOException} for convenience. |
| 14 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 15 | public class JsonProcessingException extends java.io.IOException |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 16 | { |
| 17 | final static long serialVersionUID = 123; // Stupid eclipse... |
| 18 | |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 19 | protected JsonLocation _location; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 20 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 21 | protected JsonProcessingException(String msg, JsonLocation loc, Throwable rootCause) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 22 | /* Argh. IOException(Throwable,String) is only available starting |
| 23 | * with JDK 1.6... |
| 24 | */ |
| 25 | super(msg); |
| 26 | if (rootCause != null) { |
| 27 | initCause(rootCause); |
| 28 | } |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 29 | _location = loc; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 32 | protected JsonProcessingException(String msg) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 33 | super(msg); |
| 34 | } |
| 35 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 36 | protected JsonProcessingException(String msg, JsonLocation loc) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 37 | this(msg, loc, null); |
| 38 | } |
| 39 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 40 | protected JsonProcessingException(String msg, Throwable rootCause) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 41 | this(msg, null, rootCause); |
| 42 | } |
| 43 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 44 | protected JsonProcessingException(Throwable rootCause) { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 45 | this(null, null, rootCause); |
| 46 | } |
| 47 | |
Tatu Saloranta | 388f6bd | 2012-09-28 15:49:11 -0700 | [diff] [blame] | 48 | /* |
| 49 | /********************************************************** |
| 50 | /* Extended API |
| 51 | /********************************************************** |
| 52 | */ |
| 53 | |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 54 | public JsonLocation getLocation() { return _location; } |
| 55 | |
Tatu Saloranta | 388f6bd | 2012-09-28 15:49:11 -0700 | [diff] [blame] | 56 | /** |
Alex Yursha | 71d2a01 | 2016-08-19 12:45:57 -0700 | [diff] [blame] | 57 | * Method that allows to remove context information from this exception's message. |
Tatu Saloranta | a778f95 | 2016-11-03 22:09:34 -0700 | [diff] [blame] | 58 | * Useful when you are parsing security-sensitive data and don't want original data excerpts |
| 59 | * to be present in Jackson parser error messages. |
| 60 | * |
| 61 | * @since 2.9 |
Alex Yursha | 71d2a01 | 2016-08-19 12:45:57 -0700 | [diff] [blame] | 62 | */ |
| 63 | public void clearLocation() { _location = null; } |
| 64 | |
| 65 | /** |
Tatu Saloranta | 388f6bd | 2012-09-28 15:49:11 -0700 | [diff] [blame] | 66 | * Method that allows accessing the original "message" argument, |
| 67 | * without additional decorations (like location information) |
| 68 | * that overridden {@link #getMessage} adds. |
| 69 | * |
| 70 | * @since 2.1 |
| 71 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 72 | public String getOriginalMessage() { return super.getMessage(); } |
Tatu Saloranta | 388f6bd | 2012-09-28 15:49:11 -0700 | [diff] [blame] | 73 | |
Tatu Saloranta | 77be53c | 2015-08-17 22:52:56 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Method that allows accessing underlying processor that triggered |
| 76 | * this exception; typically either {@link JsonParser} or {@link JsonGenerator} |
| 77 | * for exceptions that originate from streaming API. |
| 78 | * Note that it is possible that `null` may be returned if code throwing |
| 79 | * exception either has no access to processor; or has not been retrofitted |
| 80 | * to set it; this means that caller needs to take care to check for nulls. |
| 81 | * Subtypes override this method with co-variant return type, for more |
| 82 | * type-safe access. |
| 83 | * |
| 84 | * @return Originating processor, if available; null if not. |
| 85 | * |
| 86 | * @since 2.7 |
| 87 | */ |
| 88 | public Object getProcessor() { return null; } |
| 89 | |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 90 | /* |
| 91 | /********************************************************** |
| 92 | /* Methods for sub-classes to use, override |
| 93 | /********************************************************** |
| 94 | */ |
| 95 | |
| 96 | /** |
| 97 | * Accessor that sub-classes can override to append additional |
| 98 | * information right after the main message, but before |
| 99 | * source location information. |
| 100 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 101 | protected String getMessageSuffix() { return null; } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 102 | |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 103 | /* |
| 104 | /********************************************************** |
| 105 | /* Overrides of standard methods |
| 106 | /********************************************************** |
| 107 | */ |
| 108 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Default method overridden so that we can add location information |
| 111 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 112 | @Override public String getMessage() { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 113 | String msg = super.getMessage(); |
| 114 | if (msg == null) { |
| 115 | msg = "N/A"; |
| 116 | } |
| 117 | JsonLocation loc = getLocation(); |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 118 | String suffix = getMessageSuffix(); |
| 119 | // mild optimization, if nothing extra is needed: |
| 120 | if (loc != null || suffix != null) { |
| 121 | StringBuilder sb = new StringBuilder(100); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 122 | sb.append(msg); |
Tatu Saloranta | 8e922aa | 2012-03-19 16:51:34 -0700 | [diff] [blame] | 123 | if (suffix != null) { |
| 124 | sb.append(suffix); |
| 125 | } |
| 126 | if (loc != null) { |
| 127 | sb.append('\n'); |
| 128 | sb.append(" at "); |
| 129 | sb.append(loc.toString()); |
| 130 | } |
| 131 | msg = sb.toString(); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 132 | } |
| 133 | return msg; |
| 134 | } |
| 135 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 136 | @Override public String toString() { return getClass().getName()+": "+getMessage(); } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 137 | } |