001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.serializer;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016
017import java.lang.reflect.*;
018import java.text.*;
019import java.util.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.json.*;
023
024/**
025 * General exception thrown whenever an error occurs during serialization.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a>
029
030 * </ul>
031 *
032 * @serial exclude
033 */
034public class SerializeException extends BasicRuntimeException {
035
036   private static final long serialVersionUID = 1L;
037
038   /**
039    * Creator method.
040    *
041    * <p>
042    * If the throwable is already a {@link SerializeException}, we simply return that exception as-is.
043    * If the throwable is an {@link InvocationTargetException}, we unwrap the thrown exception.
044    * Otherwise we create a new {@link SerializeException}.
045    *
046    * @param e The exception being wrapped or unwrapped.
047    * @return A new {@link SerializeException}.
048    */
049   public static SerializeException create(Throwable e) {
050      if (e instanceof InvocationTargetException)
051         e = ((InvocationTargetException)e).getCause();
052      if (e instanceof SerializeException)
053         return (SerializeException)e;
054      return new SerializeException(e);
055   }
056
057   /**
058    * Constructor.
059    *
060    * @param session The serializer session to extract information from.
061    * @param message The exception message containing {@link MessageFormat}-style arguments.
062    * @param args Optional {@link MessageFormat}-style arguments.
063    */
064   public SerializeException(SerializerSession session, String message, Object...args) {
065      super(getMessage(session, message, args));
066   }
067
068   /**
069    * Constructor.
070    *
071    * @param message The exception message containing {@link MessageFormat}-style arguments.
072    * @param args Optional {@link MessageFormat}-style arguments.
073    */
074   public SerializeException(String message, Object...args) {
075      super(getMessage(null, message, args));
076   }
077
078   /**
079    * Constructor.
080    *
081    * @param session The serializer session to extract information from.
082    * @param causedBy The inner exception.
083    */
084   public SerializeException(SerializerSession session, Exception causedBy) {
085      super(causedBy, getMessage(session, causedBy.getMessage()));
086   }
087
088   /**
089    * Constructor.
090    *
091    * @param causedBy The inner exception.
092    */
093   public SerializeException(Throwable causedBy) {
094      super(causedBy, getMessage(null, causedBy.getMessage()));
095   }
096
097   private static String getMessage(SerializerSession session, String msg, Object... args) {
098      msg = format(msg, args);
099      if (session != null) {
100         Map<String,Object> m = session.getLastLocation();
101         if (m != null && ! m.isEmpty())
102            msg = "Serialize exception occurred at " + Json5Serializer.DEFAULT.toString(m) + ".  " + msg;
103      }
104      return msg;
105   }
106
107   /**
108    * Returns the highest-level <c>ParseException</c> in the stack trace.
109    * Useful for JUnit testing of error conditions.
110    *
111    * @return The root parse exception, or this exception if there isn't one.
112    */
113   public SerializeException getRootCause() {
114      SerializeException t = this;
115      while (! (t.getCause() == null || ! (t.getCause() instanceof SerializeException)))
116         t = (SerializeException)t.getCause();
117      return t;
118   }
119
120   /**
121    * Sets the inner cause for this exception.
122    *
123    * @param cause The inner cause.
124    * @return This object.
125    */
126   @Override /* Throwable */
127   public synchronized SerializeException initCause(Throwable cause) {
128      super.initCause(cause);
129      return this;
130   }
131}