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