001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau;
018
019import static org.apache.juneau.commons.reflect.ReflectionUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021
022import java.text.*;
023
024import org.apache.juneau.json.*;
025
026/**
027 * General invalid conversion exception.
028 *
029 * <p>
030 * Exception that gets thrown if you try to perform an invalid conversion, such as when calling
031 * {@code JsonMap.getInt(...)} on a non-numeric <c>String</c>.
032 *
033 *
034 * @serial exclude
035 */
036public class InvalidDataConversionException extends BasicRuntimeException {
037
038   private static final long serialVersionUID = 1L;
039
040   private static String name(Class<?> c) {
041      return info(c).getNameFull();
042   }
043
044   private static String name(Object o) {
045      return info(o).getNameFull();
046   }
047
048   private static String value(Object o) {
049      if (o instanceof Class o2)
050         return "'" + name(o2) + "'";
051      return Json5Serializer.DEFAULT == null ? "'" + o.toString() + "'" : Json5Serializer.DEFAULT.toString(o);
052   }
053
054   /**
055    * @param toType Attempting to convert to this class type.
056    * @param cause The cause.
057    * @param value The value being converted.
058    */
059   public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) {
060      this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''.  Value={2}.", name(value), name(toType), value(value));
061   }
062
063   /**
064    * @param toType Attempting to convert to this class type.
065    * @param cause The cause.
066    * @param value The value being converted.
067    */
068   public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) {
069      this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''.  Value={2}.", name(value), s(toType), value(value));
070   }
071
072   /**
073    * Constructor.
074    *
075    * @param cause The cause of this exception.
076    * @param message The {@link MessageFormat}-style message.
077    * @param args Optional {@link MessageFormat}-style arguments.
078    */
079   public InvalidDataConversionException(Throwable cause, String message, Object...args) {
080      super(cause, message, args);
081   }
082
083   @Override /* Overridden from BasicRuntimeException */
084   public InvalidDataConversionException setMessage(String message, Object...args) {
085      super.setMessage(message, args);
086      return this;
087   }
088}