001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019
020package lucee.runtime.exp;
021
022import lucee.runtime.op.Caster;
023import lucee.runtime.type.util.Type;
024
025
026/**
027 * 
028 */
029public class CasterException extends ExpressionException {
030
031    /**
032     * constructor of the Exception
033     * @param o
034     * @param type
035     */
036        public CasterException(Object o,String type) {
037        super(createMessage(o, type),createDetail(o));
038    }
039        
040        public CasterException(Object o,Class clazz) {
041        super(createMessage(o, Caster.toTypeName(clazz)),createDetail(o));
042    }
043
044    /**
045     * constructor of the Exception
046     * @param message
047     */
048    public CasterException(String message) {
049        super(message);
050    }
051    
052    private static String createDetail(Object o) {
053        if(o!=null) return "Java type of the object is "+Caster.toClassName(o);
054        return "value is null";
055    }   
056    
057    public static String createMessage(Object o, String type) {
058        
059        if(o instanceof String) return "Can't cast String ["+o+"] to a value of type ["+type+"]";
060        if(o!=null) return "Can't cast Object type ["+Type.getName(o)+"] to a value of type ["+type+"]";
061        return "Can't cast Null value to value of type ["+type+"]";
062    }   
063}