001 package railo.commons.lang; 002 003 import java.io.IOException; 004 import java.io.PrintWriter; 005 import java.io.StringWriter; 006 import java.lang.reflect.InvocationTargetException; 007 import java.util.Arrays; 008 009 import railo.runtime.exp.NativeException; 010 import railo.runtime.exp.PageException; 011 import railo.runtime.exp.PageExceptionImpl; 012 import railo.runtime.type.List; 013 014 public final class ExceptionUtil { 015 016 public static String getStacktrace(Throwable t, boolean addMessage) { 017 StringWriter sw = new StringWriter(); 018 PrintWriter pw = new PrintWriter(sw); 019 t.printStackTrace(pw); 020 pw.close(); 021 String st = sw.toString(); 022 String msg=t.getMessage(); 023 if(addMessage && !StringUtil.isEmpty(msg) && !st.startsWith(msg.trim())) 024 st=msg+"\n"+st; 025 return st; 026 027 } 028 029 public static PageException addHint(PageExceptionImpl pe,String hint) { 030 pe.setAdditional("Hint", hint); 031 return pe; 032 } 033 034 /** 035 * creates a message for key not found with soundex check for similar key 036 * @param keys 037 * @param keyLabel 038 * @return 039 */ 040 public static String similarKeyMessage(String[] keys,String keySearched, String keyLabel, String keyLabels) { 041 042 Arrays.sort(keys); 043 String list=List.arrayToList(keys, ","); 044 String keySearchedSoundex=StringUtil.soundex(keySearched); 045 046 for(int i=0;i<keys.length;i++){ 047 if(StringUtil.soundex(keys[i]).equals(keySearchedSoundex)) 048 return keyLabel+" ["+keySearched+"] does not exist, but there is a similar "+keyLabel+" ["+keys[i]+"] available, complete list of all available "+keyLabels+" ["+list+"]"; 049 } 050 return keyLabel+" ["+keySearched+"] does not exist, only the followings are available "+keyLabels+" ["+list+"]"; 051 } 052 053 public static IOException toIOException(Throwable t) { 054 if(t instanceof IOException) return (IOException) t; 055 if(t instanceof InvocationTargetException) return toIOException(((InvocationTargetException) t).getCause()); 056 if(t instanceof NativeException) return toIOException(((NativeException)t).getCause()); 057 058 IOException ioe = new IOException(t.getClass().getName()+":"+t.getMessage()); 059 ioe.setStackTrace(t.getStackTrace()); 060 return ioe; 061 } 062 }