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 **/
019package lucee.runtime.exp;
020
021import lucee.runtime.type.FunctionArgument;
022import lucee.runtime.type.UDF;
023import lucee.runtime.type.util.Type;
024
025public class UDFCasterException extends CasterException {
026
027        private static final long serialVersionUID = 4863042711433241644L;
028
029        public UDFCasterException(UDF udf, FunctionArgument arg, Object value, int index) {
030                super(createMessage(udf,arg,value,index));
031        }
032
033        public UDFCasterException(UDF udf, String returnType, Object value) {
034                super(createMessage(udf,returnType,value));
035        }
036
037    private static String createMessage(UDF udf, String type, Object value) {
038        String detail;
039        if(value instanceof String) return "can't cast String ["+value+"] to a value of type ["+type+"]";
040        else if(value!=null) detail= "can't cast Object type ["+Type.getName(value)+"] to a value of type ["+type+"]";
041                else detail= "can't cast null value to value of type ["+type+"]";
042                return "the function "+udf.getFunctionName()+" has an invalid return value , "+detail;
043
044    }   
045
046        private static String createMessage(UDF udf, FunctionArgument arg, Object value, int index) {
047                String detail;
048                if(value instanceof String) detail= "can't cast String ["+value+"] to a value of type ["+arg.getTypeAsString()+"]";
049                else if(value!=null) detail= "can't cast Object type ["+Type.getName(value)+"] to a value of type ["+arg.getTypeAsString()+"]";
050                else detail= "can't cast Null value to value of type ["+arg.getTypeAsString()+"]";
051                
052                
053                
054                return "invalid call of the function "+udf.getFunctionName()+" ("+udf.getPageSource().getDisplayPath()+"), "+posToString(index)+" Argument ("+arg.getName()+") is of invalid type, "+detail;
055        }
056        
057        private static String posToString(int index) {
058                if(index==1) return "first";
059                if(index==2) return "second";
060                return index+"th";
061        }
062}