Skip to content

Commit

Permalink
Improve ServiceException handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Mar 18, 2024
1 parent eb196f7 commit 482b260
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions src/main/java/fr/igred/omero/exception/ExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,59 @@ protected ExceptionHandler(T value, Exception exception) {
}


/**
* Returns {@code true} if the exception is an {@link AuthenticationException}.
*
* @param t The Exception.
*
* @return See above.
*/
private static boolean isAuthenticationException(Throwable t) {
return t != null &&
AuthenticationException.class.isAssignableFrom(t.getClass());
}


/**
* Returns {@code true} if the exception is an {@link SecurityViolation}.
*
* @param t The Exception.
*
* @return See above.
*/
private static boolean isSecurityViolation(Throwable t) {
return t instanceof SecurityViolation ||
t != null && t.getCause() instanceof SecurityViolation;
}


/**
* Returns {@code true} if the exception is a {@link ServerError}, or a {@link DSOutOfServiceException} and the
* cause is either:
* <ul><li>a {@link SecurityViolation}</li>
* <li>a {@link SessionException}</li>
* <li>an {@link AuthenticationException}</li>
* <li>a {@link ResourceError}</li></ul>
*
* @param t The Exception
*
* @return See above.
*/
private static boolean shouldBeHandled(Throwable t) {
boolean toHandle = false;
if (t instanceof ServerError) {
toHandle = true;
} else if (t instanceof DSOutOfServiceException) {
Throwable cause = t.getCause();
toHandle = isSecurityViolation(cause) ||
cause instanceof SessionException ||
isAuthenticationException(cause) ||
cause instanceof ResourceError;
}
return toHandle;
}


/**
* @deprecated Helper method to convert DSOutOfServiceException to ServiceException.
*
Expand Down Expand Up @@ -279,12 +332,14 @@ public static <I, R> R call(I input,
public static void handleOMEROException(Throwable throwable, String message)
throws ServiceException, AccessException {
Throwable cause = throwable.getCause();
if (cause instanceof SecurityViolation) {
if (isSecurityViolation(cause)) {
String s = String.format("For security reasons, cannot access data. %n");
throw new AccessException(s + message, cause);
} else if (cause instanceof SessionException ||
(cause != null && AuthenticationException.class.isAssignableFrom(cause.getClass()))) {
String s = String.format("Session is not valid or not properly initialized. %n");
} else if (cause instanceof SessionException) {
String s = String.format("Session is not valid. %n");
throw new ServiceException(s + message, cause);
} else if (isAuthenticationException(cause)) {
String s = String.format("Cannot initialize the session. %n");
throw new ServiceException(s + message, cause);
} else if (cause instanceof ResourceError) {
String s = String.format("Fatal error. Please contact the administrator. %n");
Expand Down Expand Up @@ -349,8 +404,10 @@ public <E extends Throwable> ExceptionHandler<T> rethrow(Class<E> type) throws E
*/
public ExceptionHandler<T> handleServerAndService(String message)
throws ServiceException, AccessException {
if (exception instanceof ServerError || exception instanceof DSOutOfServiceException) {
if (shouldBeHandled(exception)) {
handleOMEROException(this.exception, message);
} else if (exception instanceof DSOutOfServiceException) {
rethrow(DSOutOfServiceException.class, ServiceException::new, message);
}
return this;
}
Expand Down Expand Up @@ -435,8 +492,7 @@ public ExceptionHandler<T> handleException(String message)
*/
public ExceptionHandler<T> handleOMEROException(String message)
throws ServiceException, AccessException {
return this.rethrow(DSOutOfServiceException.class, ServiceException::new, message)
.rethrow(DSAccessException.class, AccessException::new, message)
return this.rethrow(DSAccessException.class, AccessException::new, message)
.handleServerAndService(message);
}

Expand Down

0 comments on commit 482b260

Please sign in to comment.