Error message customization according to exception thrown inside transaction

I’m developing a custom event listener SPI that interacts with an external application when some admin events take place. This interaction is mainly related with the creation, update and deletion of a representation of the users existing in keycloak, so I really need to keep consistency between keycloak and that external application. I managed to get that by wrapping the logic inside a transaction in keycloak, so that if an exception is thrown the whole action is rolled back and the data remains consistent in both places. However, everytime an exception is thrown (no matter which and with whatever message) the text shown in the console is always the same (“Error! An unexpected server error has occurred”).

I’m trying to change that message according to the exception thrown and until now I´ve tried these approaches, all without success:

  • Using exceptions that extend from javax.ws.rs.WebApplicationException, org.keycloak.services.ErrorResponseException, org.keycloak.models.ModelException or org.keycloak.storage.ReadOnlyException.
  • looking into the admin console theme to see where that message is defined in order to override it, as posted here. In this case I didn’t actually tried to override it because I didn’t find it.

Is there any way to customize this message according to the exception thrown? Or am I doing something wrong with the tried approaches?

1 Like

I’ve had success calling ‘throw ErrorResponse.error({message}, {status})’, from within an EventListenerTransaction.