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.marshall;
018
019import static org.apache.juneau.commons.reflect.ReflectionUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import org.apache.juneau.marshall.json5.*;
023
024/**
025 * General invalid conversion exception.
026 *
027 * <p>
028 * Exception that gets thrown if you try to perform an invalid conversion, such as when calling
029 * {@code JsonMap.getInt(...)} on a non-numeric <c>String</c>.
030 *
031 *
032 * @serial exclude
033 */
034@SuppressWarnings({
035   "java:S110" // Deep inheritance inherent to the exception hierarchy
036})
037public class InvalidDataConversionException extends BasicRuntimeException {
038
039   private static final long serialVersionUID = 1L;
040
041   private static String name(Class<?> c) {
042      return info(c).getNameFull();
043   }
044
045   private static String name(Object o) {
046      return info(o).getNameFull();
047   }
048
049   private static String value(Object o) {
050      if (o instanceof Class<?> o2)
051         return "'" + name(o2) + "'";
052      return Json5Serializer.DEFAULT == null ? "'" + o.toString() + "'" : Json5Serializer.DEFAULT.toString(o);
053   }
054
055   /**
056    * @param toType Attempting to convert to this class type.
057    * @param cause The cause.
058    * @param value The value being converted.
059    */
060   public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) {
061      this(cause, "Invalid data conversion from type '%s' to type '%s'.  Value=%s.", name(value), name(toType), value(value));
062   }
063
064   /**
065    * @param toType Attempting to convert to this class type.
066    * @param cause The cause.
067    * @param value The value being converted.
068    */
069   public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) {
070      this(cause, "Invalid data conversion from type '%s' to type '%s'.  Value=%s.", name(value), s(toType), value(value));
071   }
072
073   /**
074    * Constructor.
075    *
076    * @param cause The cause of this exception.  Can be <jk>null</jk> (no cause is set).
077    * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders).
078    * @param args Optional {@link String#format(String, Object...) String.format}-style arguments.
079    */
080   public InvalidDataConversionException(Throwable cause, String message, Object...args) {
081      super(cause, message, args);
082   }
083
084   @Override /* Overridden from BasicRuntimeException */
085   public InvalidDataConversionException setMessage(String message, Object...args) {
086      super.setMessage(message, args);
087      return this;
088   }
089}