Example usage for org.apache.commons.lang.exception NestableException getThrowables

List of usage examples for org.apache.commons.lang.exception NestableException getThrowables

Introduction

In this page you can find the example usage for org.apache.commons.lang.exception NestableException getThrowables.

Prototype

public Throwable[] getThrowables() 

Source Link

Usage

From source file:com.netspective.sparx.navigate.NavigationPage.java

/**
 * Try to locate the error page that can handle a given exception. First, check if we have any registered pages
 * that handle the class of the exception, then check our ancestors. If we don't handle the given exception class
 * and neither do our ancestors, check the superclass of the exception in our list and our ancestors. Keep doing
 * the check until a navigation page is found. If a page is found the navigation context's error information will
 * be appropriately set.//from  w w w. ja  v a2s  . co m
 *
 * @param t The exception that we would like to find a error page for
 *
 * @return True if we found a page, false if no page was found
 */
public boolean findErrorPage(NavigationContext nc, Throwable t) {
    if (t instanceof ServletException) {
        ServletException se = (ServletException) t;
        Throwable rootCause = se.getRootCause();
        if (rootCause != null) {
            if (findErrorPage(nc, rootCause))
                return true;
        }
    }

    // if we're dealing with a nested exception, check to see if one of the nested exceptions is something we
    // need to handle
    if (t instanceof NestableException) {
        NestableException ne = (NestableException) t;
        Throwable[] throwables = ne.getThrowables();
        for (int i = 0; i < throwables.length; i++) {
            Throwable nestedException = throwables[i];
            if (t.getClass() == nestedException.getClass()) // don't get stuck in an infinite loop
                continue;

            if (findErrorPage(nc, nestedException))
                return true;
        }
    }

    Class exceptionClass = t.getClass();
    while (exceptionClass != null) {
        for (int i = 0; i < errorPagesList.size(); i++) {
            NavigationErrorPage errorPage = (NavigationErrorPage) errorPagesList.get(i);
            if (errorPage.canHandle(t, false) || errorPage.canHandle(exceptionClass, false)) {
                nc.setErrorPageException(errorPage, t, exceptionClass);
                return true;
            }

            // check if we can handle of the interfaces of the current exception class
            Class[] interfaces = exceptionClass.getInterfaces();
            for (int intf = 0; intf < interfaces.length; intf++) {
                Class interfaceClass = interfaces[intf];
                if (errorPage.canHandle(interfaceClass, false)) {
                    nc.setErrorPageException(errorPage, t, interfaceClass);
                    return true;
                }
            }
        }

        exceptionClass = exceptionClass.getSuperclass();
        if (!Throwable.class.isAssignableFrom(exceptionClass))
            break;
    }

    NavigationPage parentPage = (NavigationPage) getParent();
    if (parentPage != null) {
        if (parentPage.findErrorPage(nc, t))
            return true;
    }

    // if we get to here, neither we nor our ancestors know how to handle this exception so plead ignorance
    return false;
}