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 org.apache.juneau.json.*; 018 019/** 020 * General invalid conversion exception. 021 * 022 * <p> 023 * Exception that gets thrown if you try to perform an invalid conversion, such as when calling 024 * {@code ObjectMap.getInt(...)} on a non-numeric <code>String</code>. 025 */ 026public final class InvalidDataConversionException extends FormattedRuntimeException { 027 028 private static final long serialVersionUID = 1L; 029 030 /** 031 * @param toType Attempting to convert to this class type. 032 * @param cause The cause. 033 * @param value The value being converted. 034 */ 035 public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) { 036 super(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", 037 getReadableClassNameForObject(value), getReadableClassName(toType), getValue(value)); 038 } 039 040 /** 041 * @param toType Attempting to convert to this class type. 042 * @param cause The cause. 043 * @param value The value being converted. 044 */ 045 public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) { 046 super(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", 047 getReadableClassNameForObject(value), toType.toString(), getValue(value)); 048 } 049 050 private static String getValue(Object o) { 051 if (o instanceof Class) 052 return "'" + getReadableClassName((Class<?>)o) + "'"; 053 return JsonSerializer.DEFAULT_LAX == null ? "'" + o.toString() + "'" : JsonSerializer.DEFAULT_LAX.toString(o); 054 } 055}