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.text.*;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.json.*;
022
023/**
024 * General exception thrown whenever an error occurs during serialization.
025 */
026public class SerializeException extends FormattedException {
027
028   private static final long serialVersionUID = 1L;
029
030   /**
031    * Constructor.
032    *
033    * @param session The serializer session to extract information from.
034    * @param message The exception message containing {@link MessageFormat}-style arguments.
035    * @param args Optional {@link MessageFormat}-style arguments.
036    */
037   public SerializeException(SerializerSession session, String message, Object...args) {
038      super(getMessage(session, message, args));
039   }
040
041   /**
042    * Constructor.
043    *
044    * @param message The exception message containing {@link MessageFormat}-style arguments.
045    * @param args Optional {@link MessageFormat}-style arguments.
046    */
047   public SerializeException(String message, Object...args) {
048      super(getMessage(null, message, args));
049   }
050
051   /**
052    * Constructor.
053    *
054    * @param session The serializer session to extract information from.
055    * @param causedBy The inner exception.
056    */
057   public SerializeException(SerializerSession session, Exception causedBy) {
058      super(causedBy, getMessage(session, causedBy.getMessage()));
059   }
060
061   /**
062    * Constructor.
063    *
064    * @param causedBy The inner exception.
065    */
066   public SerializeException(Exception causedBy) {
067      super(causedBy, getMessage(null, causedBy.getMessage()));
068   }
069
070   private static String getMessage(SerializerSession session, String msg, Object... args) {
071      msg = format(msg, args);
072      if (session != null) {
073         Map<String,Object> m = session.getLastLocation();
074         if (m != null && ! m.isEmpty())
075            msg = "Serialize exception occurred at " + SimpleJsonSerializer.DEFAULT.toString(m) + ".  " + msg;
076      }
077      return msg;
078   }
079
080   /**
081    * Returns the highest-level <code>ParseException</code> in the stack trace.
082    * Useful for JUnit testing of error conditions.
083    *
084    * @return The root parse exception, or this exception if there isn't one.
085    */
086   public SerializeException getRootCause() {
087      SerializeException t = this;
088      while (! (t.getCause() == null || ! (t.getCause() instanceof SerializeException)))
089         t = (SerializeException)t.getCause();
090      return t;
091   }
092
093   /**
094    * Sets the inner cause for this exception.
095    *
096    * @param cause The inner cause.
097    * @return This object (for method chaining).
098    */
099   @Override /* Throwable */
100   public synchronized SerializeException initCause(Throwable cause) {
101      super.initCause(cause);
102      return this;
103   }
104}