Rollback user on Registration Event result in 500 - How to display error message?

Hello there!

I wrote a custom keycloack extension that would allow me to send an HTTP request to an external system in order to populate some data on REGISTER event.

After following this thread Created user not immediately available on event I was able to have the behavior working as expected and it would look something like this:

// AuthEventListenerProvider.java

@Override
    public void onEvent(Event event) {
        try {
            switch (event.getType()) {
                case REGISTER:
                    UserRegisteredTransaction userRegisteredTransaction = new UserRegisteredTransaction(
                            cfg.getAuthUrl(),
                            cfg.getAuthKey(),
                            event.getDetails()
                    );
                    session.getTransactionManager().enlistPrepare(userRegisteredTransaction);
                    break;
            }
        } catch (Exception e) {
            log.infof("Error %s", e.toString());
        }

        log.info("-----------------------------------------------------------");
    }

The UserRegisteredTransaction implements AbstractKeycloakTransaction and do the HTTP request on commitImpl method and throws RuntimeException in case something fails.

Everything is working just fine.

The biggest problem I have is that when the user submits the registration form and my underline HTTP call fail, Keycloack returns a 500 code with no response. (See Image)

What I’d like to achieve is to simply show an Error message in the form to the user, something like:

Ops, Something went wrong try later

How can I achieve that?