View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.serializer;
18  
19  import static org.apache.juneau.commons.utils.Utils.*;
20  
21  import java.lang.reflect.*;
22  import java.text.*;
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.json.*;
27  
28  /**
29   * General exception thrown whenever an error occurs during serialization.
30   *
31   * <h5 class='section'>See Also:</h5><ul>
32   * 	<li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
33  
34   * </ul>
35   *
36   * @serial exclude
37   */
38  public class SerializeException extends BasicRuntimeException {
39  
40  	private static final long serialVersionUID = 1L;
41  
42  	/**
43  	 * Creator method.
44  	 *
45  	 * <p>
46  	 * If the throwable is already a {@link SerializeException}, we simply return that exception as-is.
47  	 * If the throwable is an {@link InvocationTargetException}, we unwrap the thrown exception.
48  	 * Otherwise we create a new {@link SerializeException}.
49  	 *
50  	 * @param e The exception being wrapped or unwrapped.
51  	 * @return A new {@link SerializeException}.
52  	 */
53  	public static SerializeException create(Throwable e) {
54  		if (e instanceof InvocationTargetException e2)
55  			e = e2.getCause();
56  		if (e instanceof SerializeException e2)
57  			return e2;
58  		return new SerializeException(e);
59  	}
60  
61  	private static String getMessage(SerializerSession session, String msg, Object...args) {
62  		msg = f(msg, args);
63  		if (nn(session)) {
64  			Map<String,Object> m = session.getLastLocation();
65  			if (nn(m) && ! m.isEmpty())
66  				msg = "Serialize exception occurred at " + Json5Serializer.DEFAULT.toString(m) + ".  " + msg;
67  		}
68  		return msg;
69  	}
70  
71  	/**
72  	 * Constructor.
73  	 *
74  	 * @param session The serializer session to extract information from.
75  	 * @param causedBy The inner exception.
76  	 */
77  	public SerializeException(SerializerSession session, Exception causedBy) {
78  		super(causedBy, getMessage(session, causedBy.getMessage()));
79  	}
80  
81  	/**
82  	 * Constructor.
83  	 *
84  	 * @param session The serializer session to extract information from.
85  	 * @param message The exception message containing {@link MessageFormat}-style arguments.
86  	 * @param args Optional {@link MessageFormat}-style arguments.
87  	 */
88  	public SerializeException(SerializerSession session, String message, Object...args) {
89  		super(getMessage(session, message, args));
90  	}
91  
92  	/**
93  	 * Constructor.
94  	 *
95  	 * @param message The exception message containing {@link MessageFormat}-style arguments.
96  	 * @param args Optional {@link MessageFormat}-style arguments.
97  	 */
98  	public SerializeException(String message, Object...args) {
99  		super(getMessage(null, message, args));
100 	}
101 
102 	/**
103 	 * Constructor.
104 	 *
105 	 * @param causedBy The inner exception.
106 	 */
107 	public SerializeException(Throwable causedBy) {
108 		super(causedBy, getMessage(null, causedBy.getMessage()));
109 	}
110 
111 	/**
112 	 * Returns the highest-level <c>ParseException</c> in the stack trace.
113 	 * Useful for JUnit testing of error conditions.
114 	 *
115 	 * @return The root parse exception, or this exception if there isn't one.
116 	 */
117 	public SerializeException getRootCause() {
118 		SerializeException t = this;
119 		while (! (t.getCause() == null || ! (t.getCause() instanceof SerializeException)))
120 			t = (SerializeException)t.getCause();
121 		return t;
122 	}
123 
124 	/**
125 	 * Sets the inner cause for this exception.
126 	 *
127 	 * @param cause The inner cause.
128 	 * @return This object.
129 	 */
130 	@Override /* Overridden from Throwable */
131 	public synchronized SerializeException initCause(Throwable cause) {
132 		super.initCause(cause);
133 		return this;
134 	}
135 
136 	@Override /* Overridden from BasicRuntimeException */
137 	public SerializeException setMessage(String message, Object...args) {
138 		super.setMessage(message, args);
139 		return this;
140 	}
141 }