001    package railo.runtime.exp;
002    
003    import railo.runtime.type.FunctionArgument;
004    import railo.runtime.type.UDF;
005    import railo.runtime.type.UDFImpl;
006    import railo.runtime.type.util.Type;
007    
008    public class UDFCasterException extends CasterException {
009    
010    
011            public UDFCasterException(UDF udf, FunctionArgument arg, Object value, int index) {
012                    super(createMessage(udf,arg,value,index));
013            }
014    
015            public UDFCasterException(UDFImpl udf, String returnType, Object value) {
016                    super(createMessage(udf,returnType,value));
017            }
018    
019        private static String createMessage(UDFImpl udf, String type, Object value) {
020            String detail;
021            if(value instanceof String) return "can't cast String ["+value+"] to a value of type ["+type+"]";
022            else if(value!=null) detail= "can't cast Object type ["+Type.getName(value)+"] to a value of type ["+type+"]";
023                    else detail= "can't cast null value to value of type ["+type+"]";
024                    return "the function "+udf.getFunctionName()+" has an invalid return value , "+detail;
025    
026        }   
027    
028            private static String createMessage(UDF udf, FunctionArgument arg, Object value, int index) {
029                    String detail;
030                    if(value instanceof String) detail= "can't cast String ["+value+"] to a value of type ["+arg.getTypeAsString()+"]";
031                    else if(value!=null) detail= "can't cast Object type ["+Type.getName(value)+"] to a value of type ["+arg.getTypeAsString()+"]";
032                    else detail= "can't cast Null value to value of type ["+arg.getTypeAsString()+"]";
033                    
034                    
035                    
036                    return "invalid call of the function "+udf.getFunctionName()+" ("+udf.getPageSource().getDisplayPath()+"), "+posToString(index)+" Argument ("+arg.getName()+") is of invalid type, "+detail;
037            }
038            
039            private static String posToString(int index) {
040                    if(index==1) return "first";
041                    if(index==2) return "second";
042                    return index+"th";
043            }
044    }