001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.convert;
018
019import org.springframework.util.ObjectUtils;
020
021/**
022 * Exception to be thrown when an actual type conversion attempt fails.
023 *
024 * @author Keith Donald
025 * @author Juergen Hoeller
026 * @since 3.0
027 */
028@SuppressWarnings("serial")
029public class ConversionFailedException extends ConversionException {
030
031        private final TypeDescriptor sourceType;
032
033        private final TypeDescriptor targetType;
034
035        private final Object value;
036
037
038        /**
039         * Create a new conversion exception.
040         * @param sourceType the value's original type
041         * @param targetType the value's target type
042         * @param value the value we tried to convert
043         * @param cause the cause of the conversion failure
044         */
045        public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
046                super("Failed to convert from type [" + sourceType + "] to type [" + targetType +
047                                "] for value '" + ObjectUtils.nullSafeToString(value) + "'", cause);
048                this.sourceType = sourceType;
049                this.targetType = targetType;
050                this.value = value;
051        }
052
053
054        /**
055         * Return the source type we tried to convert the value from.
056         */
057        public TypeDescriptor getSourceType() {
058                return this.sourceType;
059        }
060
061        /**
062         * Return the target type we tried to convert the value to.
063         */
064        public TypeDescriptor getTargetType() {
065                return this.targetType;
066        }
067
068        /**
069         * Return the offending value.
070         */
071        public Object getValue() {
072                return this.value;
073        }
074
075}