I am running Keycloak in a docker container and wrote a SPI, which provides some custom rest endpoints. I compiled the code, placed the jar in the Keycloak providers folder and everything works fine. Now I want to make a global exception handler and I can not find anything in the documentation or online that works.
I wrote a GlobalExceptionMapper like this:
import com.b.keycloak.presenter.ErrorPresenter;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.jboss.logging.Logger;
@Provider
public class GlobalExceptionMapper implements ExceptionMapper<Exception> {
private static final Logger logger = Logger.getLogger(GlobalExceptionMapper.class);
@Override
public Response toResponse(Exception exception) {
logger.error("Exception occurred: ", exception);
if (exception instanceof RuntimeException) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
ErrorPresenter.makeError(exception.getMessage(), "resolution", "endpoint", "method"))
.build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(
ErrorPresenter.makeError(exception.getMessage(), "resolution", "endpoint", "method"))
.build();
}
}
}
and in my RealmResourceProviderFactory I tried to register it:
public class MyCustomProviderFactory implements RealmResourceProviderFactory {
@Override
public void init(Config.Scope config) {
// Any initialization code
}
@Override
public void postInit(KeycloakSessionFactory factory) {
ResteasyProviderFactory.getInstance().registerProvider(GlobalExceptionMapper.class);
}
}
but this does absolutely nothing and everything else I found online does absolutely nothing. Any advice?