mirror of
https://github.com/keycloak/keycloak.git
synced 2025-12-19 05:20:21 -06:00
Adding utility class for working with throwables and updating the cause check to limit the number of iterations on the stacktrace
Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package org.keycloak.common.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility class for working with {@link Throwable} instances.
|
||||||
|
*/
|
||||||
|
public final class Throwables {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given {@code throwable} or any of its causes (up to a depth of 3) is an instance of any of the specified exception types.
|
||||||
|
*
|
||||||
|
* @param throwable
|
||||||
|
* @param type
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@SafeVarargs
|
||||||
|
public static boolean isCausedBy(Throwable throwable, Class<? extends Exception>... type) {
|
||||||
|
Objects.requireNonNull(throwable, "Throwable must not be null");
|
||||||
|
int limit = 3;
|
||||||
|
List<Class<? extends Exception>> types = Arrays.asList(type);
|
||||||
|
Throwable cause = throwable.getCause();
|
||||||
|
|
||||||
|
while (cause != null) {
|
||||||
|
if (limit-- == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (types.contains(cause.getClass())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
cause = cause.getCause();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,6 +49,7 @@ import java.util.List;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static java.util.Collections.unmodifiableSet;
|
import static java.util.Collections.unmodifiableSet;
|
||||||
|
import static org.keycloak.common.util.Throwables.isCausedBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default IdentityQuery implementation.
|
* Default IdentityQuery implementation.
|
||||||
@@ -186,17 +187,9 @@ public class LDAPQuery implements AutoCloseable {
|
|||||||
result.add(ldapObject);
|
result.add(ldapObject);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Check if this is an LDAP connectivity issue
|
if (isCausedBy(e, NameNotFoundException.class, CommunicationException.class, AuthenticationException.class)) {
|
||||||
Throwable current = e;
|
throw new StorageUnavailableException("LDAP server is unavailable for provider [" + ldapFedProvider.getModel().getName() + "]", e);
|
||||||
while (current != null) {
|
|
||||||
if (current instanceof NameNotFoundException || current instanceof CommunicationException ||
|
|
||||||
current instanceof AuthenticationException) {
|
|
||||||
throw new StorageUnavailableException("LDAP server is unavailable for provider [" +
|
|
||||||
ldapFedProvider.getModel().getName() + "]", e);
|
|
||||||
}
|
|
||||||
current = current.getCause();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ModelException("Failed to fetch results from the LDAP [" + ldapFedProvider.getModel().getName() + "] provider", e);
|
throw new ModelException("Failed to fetch results from the LDAP [" + ldapFedProvider.getModel().getName() + "] provider", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package org.keycloak.models;
|
package org.keycloak.models;
|
||||||
|
|
||||||
import java.util.List;
|
import org.keycloak.common.util.Throwables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||||
@@ -52,20 +52,6 @@ public class ModelException extends RuntimeException {
|
|||||||
|
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public final boolean isCausedBy(Class<? extends Exception>... type) {
|
public final boolean isCausedBy(Class<? extends Exception>... type) {
|
||||||
int limit = 3;
|
return Throwables.isCausedBy(this, type);
|
||||||
List<Class<? extends Exception>> types = List.of(type);
|
|
||||||
Throwable cause = getCause();
|
|
||||||
|
|
||||||
while (cause != null) {
|
|
||||||
if (limit-- == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (types.contains(cause.getClass())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
cause = cause.getCause();
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user