下面列出了org.apache.commons.lang3.exception.ExceptionUtils#indexOfThrowable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
}
}
@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);
}
}
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;
}
}
/**
* 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);
}
}
/**
* Returns {@code true} if the given throwable is or is not caused by a database constraint violation.
*
* @param exception - throwable to check.
*
* @return {@code true} if is constraint violation, {@code false} otherwise.
*/
private boolean isCausedByConstraintViolationException(Exception exception)
{
// some databases will throw ConstraintViolationException
boolean isConstraintViolation = ExceptionUtils.indexOfThrowable(exception, ConstraintViolationException.class) != -1;
// other databases will not throw a nice exception
if (!isConstraintViolation)
{
// We must manually check the error codes
Throwable rootThrowable = getRootCause(exception);
if (rootThrowable instanceof SQLException)
{
SQLException sqlException = (SQLException) rootThrowable;
if (POSTGRES_SQL_STATE_CODE_FOREIGN_KEY_VIOLATION.equals(sqlException.getSQLState())
|| POSTGRES_SQL_STATE_CODE_UNIQUE_INDEX_OR_PRIMARY_KEY_VIOLATION.equals(sqlException.getSQLState()))
{
isConstraintViolation = true;
}
}
}
return isConstraintViolation;
}
@Override
public void run() {
while (true) {
try {
runner.call();
}
catch (Exception e) {
if (ExceptionUtils.indexOfThrowable(e, InterruptedException.class) != -1) {
LOGGER.info("Interrupted, exiting", e);
Thread.currentThread().interrupt();
return;
}
LOGGER.error("Hit exception while writing", e);
}
}
}
@Override
@SuppressWarnings("unchecked")
public final <T> T call(String utilityName, Object... args) throws UtilityCallException, UtilityNotFoundException, InterruptedException {
int maxCompatibilityIndex = -1;
Method bestMethod = null;
for(Method utilityMethod : utilityMethods.get(utilityName)) {
int compatibilityIndex = UtilityManagerUtils.getCompatibilityIndex(utilityMethod, args);
if(compatibilityIndex > maxCompatibilityIndex) {
maxCompatibilityIndex = compatibilityIndex;
bestMethod = utilityMethod;
}
}
if(maxCompatibilityIndex == -1) {
throw new UtilityNotFoundException(getSignature(utilityName, args));
}
try {
return (T)bestMethod.invoke(this, UtilityManagerUtils.getReflectionArgs(bestMethod, args));
} catch(Exception e) {
int interruptedExceptionIndex = ExceptionUtils.indexOfThrowable(e, InterruptedException.class);
if(interruptedExceptionIndex != -1) {
throw (InterruptedException)ExceptionUtils.getThrowableList(e).get(interruptedExceptionIndex);
} else {
if(e instanceof InvocationTargetException) {
throw new UtilityCallException(e.getCause());
}
throw new UtilityCallException(e);
}
}
}
private <T> T handleAuthException(Supplier<T> function) {
try {
return function.get();
} catch (RuntimeException e) {
if (ExceptionUtils.indexOfThrowable(e, AuthenticationException.class) != -1) {
throw new ProviderAuthenticationFailedException(e);
} else {
throw e;
}
}
}
private void handleAuthException(Runnable function) {
try {
function.run();
} catch (RuntimeException e) {
if (ExceptionUtils.indexOfThrowable(e, AuthenticationException.class) != -1) {
throw new ProviderAuthenticationFailedException(e);
} else {
throw e;
}
}
}
private boolean hasTlsException(Throwable e) {
return ExceptionUtils.indexOfThrowable(e, SSLHandshakeException.class) != -1;
}