blob: 23599b1e95f318261059e7b3cf8d9ed3185e55b2 [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
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 Saloranta32e4e912014-01-26 19:59:28 -080015public class JsonProcessingException extends java.io.IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -080016{
17 final static long serialVersionUID = 123; // Stupid eclipse...
18
Tatu Saloranta8e922aa2012-03-19 16:51:34 -070019 protected JsonLocation _location;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080020
Tatu Saloranta32e4e912014-01-26 19:59:28 -080021 protected JsonProcessingException(String msg, JsonLocation loc, Throwable rootCause) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080022 /* 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 Saloranta8e922aa2012-03-19 16:51:34 -070029 _location = loc;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080030 }
31
Tatu Saloranta32e4e912014-01-26 19:59:28 -080032 protected JsonProcessingException(String msg) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080033 super(msg);
34 }
35
Tatu Saloranta32e4e912014-01-26 19:59:28 -080036 protected JsonProcessingException(String msg, JsonLocation loc) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080037 this(msg, loc, null);
38 }
39
Tatu Saloranta32e4e912014-01-26 19:59:28 -080040 protected JsonProcessingException(String msg, Throwable rootCause) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080041 this(msg, null, rootCause);
42 }
43
Tatu Saloranta32e4e912014-01-26 19:59:28 -080044 protected JsonProcessingException(Throwable rootCause) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080045 this(null, null, rootCause);
46 }
47
Tatu Saloranta388f6bd2012-09-28 15:49:11 -070048 /*
49 /**********************************************************
50 /* Extended API
51 /**********************************************************
52 */
53
Tatu Saloranta77be53c2015-08-17 22:52:56 -070054 public JsonLocation getLocation() { return _location; }
55
Tatu Saloranta388f6bd2012-09-28 15:49:11 -070056 /**
Alex Yursha71d2a012016-08-19 12:45:57 -070057 * Method that allows to remove context information from this exception's message.
Tatu Salorantaa778f952016-11-03 22:09:34 -070058 * 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 Yursha71d2a012016-08-19 12:45:57 -070062 */
63 public void clearLocation() { _location = null; }
64
65 /**
Tatu Saloranta388f6bd2012-09-28 15:49:11 -070066 * 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 Saloranta32e4e912014-01-26 19:59:28 -080072 public String getOriginalMessage() { return super.getMessage(); }
Tatu Saloranta388f6bd2012-09-28 15:49:11 -070073
Tatu Saloranta77be53c2015-08-17 22:52:56 -070074 /**
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 Saloranta8e922aa2012-03-19 16:51:34 -070090 /*
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 Saloranta32e4e912014-01-26 19:59:28 -0800101 protected String getMessageSuffix() { return null; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800102
Tatu Saloranta8e922aa2012-03-19 16:51:34 -0700103 /*
104 /**********************************************************
105 /* Overrides of standard methods
106 /**********************************************************
107 */
108
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800109 /**
110 * Default method overridden so that we can add location information
111 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800112 @Override public String getMessage() {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800113 String msg = super.getMessage();
114 if (msg == null) {
115 msg = "N/A";
116 }
117 JsonLocation loc = getLocation();
Tatu Saloranta8e922aa2012-03-19 16:51:34 -0700118 String suffix = getMessageSuffix();
119 // mild optimization, if nothing extra is needed:
120 if (loc != null || suffix != null) {
121 StringBuilder sb = new StringBuilder(100);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800122 sb.append(msg);
Tatu Saloranta8e922aa2012-03-19 16:51:34 -0700123 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 Salorantaf15531c2011-12-22 23:00:40 -0800132 }
133 return msg;
134 }
135
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800136 @Override public String toString() { return getClass().getName()+": "+getMessage(); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800137}