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.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 */
027public class SerializeException extends BasicException {
028
029   private static final long serialVersionUID = 1L;
030
031   /**
032    * Creator method.
033    *
034    * <p>
035    * If the throwable is already a {@link SerializeException}, we simply return that exception as-is.
036    * If the throwable is an {@link InvocationTargetException}, we unwrap the thrown exception.
037    * Otherwise we create a new {@link SerializeException}.
038    *
039    * @param e The exception being wrapped or unwrapped.
040    * @return A new {@link SerializeException}.
041    */
042   public static SerializeException create(Throwable e) {
043      if (e instanceof InvocationTargetException)
044         e = ((InvocationTargetException)e).getCause();
045      if (e instanceof SerializeException)
046         return (SerializeException)e;
047      return new SerializeException(e);
048   }
049
050   /**
051    * Constructor.
052    *
053    * @param session The serializer session to extract information from.
054    * @param message The exception message containing {@link MessageFormat}-style arguments.
055    * @param args Optional {@link MessageFormat}-style arguments.
056    */
057   public SerializeException(SerializerSession session, String message, Object...args) {
058      super(getMessage(session, message, args));
059   }
060
061   /**
062    * Constructor.
063    *
064    * @param message The exception message containing {@link MessageFormat}-style arguments.
065    * @param args Optional {@link MessageFormat}-style arguments.
066    */
067   public SerializeException(String message, Object...args) {
068      super(getMessage(null, message, args));
069   }
070
071   /**
072    * Constructor.
073    *
074    * @param session The serializer session to extract information from.
075    * @param causedBy The inner exception.
076    */
077   public SerializeException(SerializerSession session, Exception causedBy) {
078      super(causedBy, getMessage(session, causedBy.getMessage()));
079   }
080
081   /**
082    * Constructor.
083    *
084    * @param causedBy The inner exception.
085    */
086   public SerializeException(Throwable causedBy) {
087      super(causedBy, getMessage(null, causedBy.getMessage()));
088   }
089
090   private static String getMessage(SerializerSession session, String msg, Object... args) {
091      msg = format(msg, args);
092      if (session != null) {
093         Map<String,Object> m = session.getLastLocation();
094         if (m != null && ! m.isEmpty())
095            msg = "Serialize exception occurred at " + SimpleJsonSerializer.DEFAULT.toString(m) + ".  " + msg;
096      }
097      return msg;
098   }
099
100   /**
101    * Returns the highest-level <c>ParseException</c> in the stack trace.
102    * Useful for JUnit testing of error conditions.
103    *
104    * @return The root parse exception, or this exception if there isn't one.
105    */
106   public SerializeException getRootCause() {
107      SerializeException t = this;
108      while (! (t.getCause() == null || ! (t.getCause() instanceof SerializeException)))
109         t = (SerializeException)t.getCause();
110      return t;
111   }
112
113   /**
114    * Sets the inner cause for this exception.
115    *
116    * @param cause The inner cause.
117    * @return This object (for method chaining).
118    */
119   @Override /* Throwable */
120   public synchronized SerializeException initCause(Throwable cause) {
121      super.initCause(cause);
122      return this;
123   }
124}