View Javadoc

1   /**
2    * 2010, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.presentation.entities.system.error;
7   
8   import javax.servlet.ServletException;
9   
10  /**
11   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
12   * @created 2010/08/11
13   */
14  public abstract class AbstractErrorHandler {
15  
16      /**
17       * @param <T>
18       *            the generic exception class to find
19       * @param exceptionClassToFind
20       * @param exception
21       * @return the exception within the stack or null if not found
22       */
23      @SuppressWarnings("unchecked")
24      public <T extends Exception> T getExceptionWithinStack(Class<T> exceptionClassToFind, Exception exception)
25      {
26          if (exception != null && exception instanceof ServletException)
27              exception = (Exception) ((ServletException) exception).getRootCause();
28  
29          while (exception != null)
30              if (exceptionClassToFind.isAssignableFrom(exception.getClass()))
31                  break;
32              else
33                  exception = (T) exception.getCause();
34  
35          if (exception != null)
36              return (T) exception;
37  
38          return null;
39      }
40  }