Java Code Examples for org.apache.commons.lang3.exception.ExceptionUtils#indexOfType()

The following examples show how to use org.apache.commons.lang3.exception.ExceptionUtils#indexOfType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCaller.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> T call(Method method, Object... args) throws InterruptedException {
    try {
        return (T)method.invoke(this, args);
    } catch(Exception e) {
        int interruptedExceptionIndex = ExceptionUtils.indexOfThrowable(e, InterruptedException.class);

        if(interruptedExceptionIndex != -1) {
            throw (InterruptedException)ExceptionUtils.getThrowableList(e).get(interruptedExceptionIndex);
        } else {
            int knownBugExceptionIndex = ExceptionUtils.indexOfType(e, KnownBugException.class);

            if (knownBugExceptionIndex != -1) {
                throw (KnownBugException)ExceptionUtils.getThrowableList(e).get(knownBugExceptionIndex);
            } else {
                if(e instanceof InvocationTargetException) {
                    throw new ActionCallException(e.getCause());
                }

                throw new ActionCallException(e);
            }
        }
    }
}
 
Example 2
Source File: DatabaseExceptionHelper.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Checks if the throwable was caused by timeout exception.</p>
 * <b>This method has been tested for Oracle and MySQL only and might not work
 * for other DB engines.</b>
 *
 * @param throwable to check
 * @return true if the throwable is caused by a timeout, false otherwise
 */
public boolean isCausedByTimeoutException(Throwable throwable) {
  // Valid test for Oracle timeout exception and some (not all!) MySQL
  // exceptions.
  if (ExceptionUtils.indexOfType(throwable, SQLTimeoutException.class) != -1) {
    return true;
  }
  // MySQL database has two timeout exceptions in two packages. One of them
  // doesn't extend SQLTimeoutException but only SQLException. It is therefore
  // necessary to do ugly name check...
  for (Throwable causeThrowable : ExceptionUtils.getThrowables(throwable)) {
    if (MYSQL_TIMEOUT_EXCEPTION_NAME.equals(causeThrowable.getClass().getSimpleName())) {
      return true;
    }
  }
  return false;
}
 
Example 3
Source File: WebTimer.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(CubaTimer sender) {
    try {
        listener.accept(new TimerActionEvent(WebTimer.this));
    } catch (RuntimeException e) {
        int reIdx = ExceptionUtils.indexOfType(e, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(e).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer {}, timer will be stopped", id != null ? id : "<noid>");
                    stop();
                    return;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(e, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer {}, timer will be stopped", id != null ? id : "<noid>");
            stop();
            return;
        }

        throw new RuntimeException("Exception on timer action", e);
    }
}
 
Example 4
Source File: DesktopTimer.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void handleTimerException(RuntimeException ex) {
    if (ExceptionUtils.indexOfType(ex, java.net.ConnectException.class) > -1) {
        // If a ConnectException occurred, just log it and ignore
        log.warn("onTimer error: " + ex.getMessage());
    } else {
        // Otherwise throw the exception, but first search for NoUserSessionException in chain,
        // if found - stop the timer
        int reIdx = ExceptionUtils.indexOfType(ex, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(ex).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                //noinspection ThrowableResultOfMethodCallIgnored
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer, timer will be stopped");
                    disposeTimer();
                    break;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(ex, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer, timer will be stopped");
            disposeTimer();
        }

        throw ex;
    }
}
 
Example 5
Source File: HerdErrorInformationExceptionHandler.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Handle Activiti exceptions. Note that this method properly handles a null response being passed in.
 *
 * @param exception the exception.
 * @param response the response.
 *
 * @return the error information.
 */
@ExceptionHandler(value = ActivitiException.class)
@ResponseBody
public ErrorInformation handleActivitiException(Exception exception, HttpServletResponse response)
{
    if ((ExceptionUtils.indexOfThrowable(exception, ActivitiClassLoadingException.class) != -1) ||
        (ExceptionUtils.indexOfType(exception, ELException.class) != -1))
    {
        // These exceptions are caused by invalid workflow configurations (i.e. user error) so they are considered a bad request.
        return getErrorInformationAndSetStatus(HttpStatus.BAD_REQUEST, exception, response);
    }
    else
    {
        // For all other exceptions, something is wrong that we weren't expecting so we'll return this as an internal server error and log the error.
        logError("An Activiti error occurred.", exception);
        return getErrorInformationAndSetStatus(HttpStatus.INTERNAL_SERVER_ERROR, exception, response);
    }
}
 
Example 6
Source File: NetworkRetryPolicy.java    From android-atleap with Apache License 2.0 6 votes vote down vote up
@Override
public void retry(SpiceException e) {
    if (ExceptionUtils.indexOfType(e, ServerErrorException.class) >= 0) {
        //in case of 200 response and serverError like incorrect params
        //do not retry server errors
        retryCount = 0;
    } else if (ExceptionUtils.indexOfType(e, UnauthorizedException.class) >= 0) {
        //Special case for unauthorized error. Retry only one time.
        if (retryCount > 1)
            retryCount = 1;
        else
            retryCount--;

        backOffMultiplier = 0;
    } else {
        //default behaviour in case of NoNetwork error or server response with code >= 300
        retryCount--;
    }

    delayBeforeRetry = (long) (delayBeforeRetry * backOffMultiplier);
}
 
Example 7
Source File: TlsErrorCheck.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
public ResponseEntity<ApiMessageView> checkError(HttpServletRequest request, Throwable exc) {
    if (exc instanceof ZuulException) {
        int exceptionIndex = ExceptionUtils.indexOfType(exc, SSLException.class);
        if (exceptionIndex != -1) {
            Throwable sslException = ExceptionUtils.getThrowables(exc)[exceptionIndex];
            log.debug("TLS request error: {}", sslException.getMessage(), sslException);
            return tlsErrorResponse(request, sslException.getMessage());
        }
    }

    return null;
}
 
Example 8
Source File: NetworkErrorHandler.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
@Override
public Throwable handleError(RetrofitError retrofitError) {
    if (retrofitError.isNetworkError()) {
        Log.w(TAG, "Cannot connect to " + retrofitError.getUrl());
        return new NoNetworkException();
    }


    Response response = retrofitError.getResponse();
    if (response != null) {
        int status = response.getStatus();
        if (status == 401) {
            //throw our own exception about unauthorized access
            Log.w(TAG, "Access in not authorized " + retrofitError.getUrl());
            Context context = AppContext.getContext();
            AuthHelper.reCreateAuthTokenForLastAccountBlocking(context, Constants.ACCOUNT_TYPE, Constants.ACCOUNT_TOKEN_TYPE, null, null, null);
            return new UnauthorizedException("Access in not authorized " + retrofitError.getUrl(), retrofitError);
        } else if (status >= 300) {
            Log.w(TAG, "Error " + String.valueOf(status) + " while accessing " + retrofitError.getUrl());
            return retrofitError;
        }
    }

    int index = ExceptionUtils.indexOfType(retrofitError, ServerErrorException.class);

    if (index >= 0) {
        List<Throwable> errorList = ExceptionUtils.getThrowableList(retrofitError);
        ServerErrorException serverErrorException = (ServerErrorException)errorList.get(index);
        if (serverErrorException instanceof DeveloperErrorException) {
            Log.e(TAG, "Developer error with code" + serverErrorException.getErrorCode(), serverErrorException);
        }
        return serverErrorException;
    }

    return retrofitError;
}
 
Example 9
Source File: AuthActivity.java    From android-atleap with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestFailure(SpiceException e) {
    changeProgressBarVisibility(false);
    if (e instanceof NoNetworkException) {
        Toast.makeText(AuthActivity.this, R.string.no_network, Toast.LENGTH_LONG).show();
    } else if (ExceptionUtils.indexOfType(e, ServerErrorException.class) >= 0) {
        Toast.makeText(AuthActivity.this, R.string.activity_auth_cannot_login, Toast.LENGTH_LONG).show();
    }
}