001    package com.softwarementors.extjs.djn;
002    
003    import java.io.PrintWriter;
004    import java.io.StringWriter;
005    import java.io.Writer;
006    import java.lang.reflect.InvocationTargetException;
007    import java.util.ArrayList;
008    import java.util.List;
009    
010    import com.softwarementors.extjs.djn.router.dispatcher.MethodExecutionException;
011    
012    public class ExceptionUtils {
013      private ExceptionUtils() {
014        // Avoid instantiation
015      }
016      
017      public static List<Throwable> getRelevantExceptions(Throwable e) {
018        List<Throwable> result = new ArrayList<Throwable>();
019        Throwable t = e;
020        while( t != null ) {
021          if( !skipException(t)) {
022            result.add(t);
023          }
024          t = t.getCause();
025        }
026        return result;
027      }
028      
029      public static boolean skipException( Throwable t ) {
030        assert t != null;
031        return t instanceof MethodExecutionException || t instanceof InvocationTargetException;
032      }
033      
034      public static Throwable getFirstRelevantExceptionToReport(Throwable t) {
035        assert t != null;
036        
037        Throwable reportedException = t;
038        while( skipException(reportedException) ) {
039          reportedException = reportedException.getCause();
040          assert reportedException != null;
041        }
042        return reportedException;
043      }
044    
045      public static String getExceptionMessage(Throwable t) {
046        assert t!= null;
047    
048        String result = ClassUtils.getSimpleName( t.getClass() );
049        if( t.getMessage() != null ) {
050          result += ": " + t.getMessage();
051        }
052        return result;
053      }
054    
055      public static String getExceptionWhere(Throwable t, boolean debugMode) {
056        assert t != null;
057        
058        if( debugMode ) {
059          Writer where = new StringWriter();
060          PrintWriter printWriter = new PrintWriter( where );
061          t.printStackTrace(printWriter);
062          return where.toString();
063        }
064        return "";
065      }
066    
067    }