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;
014
015import static org.apache.juneau.internal.ClassUtils.*;
016
017import java.text.*;
018
019import org.apache.juneau.json.*;
020
021/**
022 * General invalid conversion exception.
023 *
024 * <p>
025 * Exception that gets thrown if you try to perform an invalid conversion, such as when calling
026 * {@code ObjectMap.getInt(...)} on a non-numeric <code>String</code>.
027 */
028public final class InvalidDataConversionException extends FormattedRuntimeException {
029
030   private static final long serialVersionUID = 1L;
031
032   /**
033    * Constructor.
034    *
035    * @param cause The cause of this exception.
036    * @param message The {@link MessageFormat}-style message.
037    * @param args Optional {@link MessageFormat}-style arguments.
038    */
039   public InvalidDataConversionException(Throwable cause, String message, Object... args) {
040      super(cause, getMessage(cause, message, null), args);
041   }
042
043   /**
044    * @param toType Attempting to convert to this class type.
045    * @param cause The cause.
046    * @param value The value being converted.
047    */
048   public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) {
049      this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''.  Value={2}.",
050         getReadableClassNameForObject(value), getReadableClassName(toType), getValue(value));
051   }
052
053   /**
054    * @param toType Attempting to convert to this class type.
055    * @param cause The cause.
056    * @param value The value being converted.
057    */
058   public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) {
059      this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''.  Value={2}.",
060         getReadableClassNameForObject(value), toType.toString(), getValue(value));
061   }
062
063   private static String getValue(Object o) {
064      if (o instanceof Class)
065         return "'" + getReadableClassName((Class<?>)o) + "'";
066      return SimpleJsonSerializer.DEFAULT == null ? "'" + o.toString() + "'" : SimpleJsonSerializer.DEFAULT.toString(o);
067   }
068}