Error handling when starting the keycloak

See original GitHub issue

Bug Report or Feature Request

- [ X ] bug report 
- [ ] feature request

Versions.

Angular: 9.0.1 Keycloa and keycloak-jsk: 11.0.2 Keycloak-angular: 8.0.1

Repro steps.

Simple follow the README config steps.

Desired functionality.

I would like to know if there is any way to handle errors when starting the keycloak. When the keycloak server is unavailable, for example. I did several tests and under no circumstances was I able to deal with this scenario, the application simply breaks and nothing happens.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:15

github_iconTop GitHub Comments

3reactions
gatos-cccommented, Feb 7, 2022

Anyone have a working example of this? This is one of the top hits in Google when searching for error handling during keycloak-angular init (and most of the others eventually land you here). I followed the PRs listed above, and made sure I’m using keycloak-js > 15.0.0:

    "keycloak-angular": "~9.1.0",
    "keycloak-js": "~16.1.1",

and my init code:

export function initializeKeycloak(keycloak: KeycloakService) {
  return () => {
    let storedConfig: any = sessionStorage.getItem('kcc');
    if (storedConfig != null) {
      console.log("Initializing Keycloak client with config: " + storedConfig);
      let keycloakConfig = JSON.parse(storedConfig);

      // Wrapping in a try/catch - if the values provided aren't valid, or
      // the keycloak server isn't available, the init function will bomb out
      // the entire site (entire page will be blank and unusable).
      try {
        let init = keycloak.init({
          config: {
            url: keycloakConfig.url,
            realm: keycloakConfig.realm,
            clientId: keycloakConfig.resource
          },
          initOptions: {
            // onLoad: 'check-sso',
            // silentCheckSsoRedirectUri:
            //   window.location.origin + '/assets/silent-check-sso.html',
            // checkLoginIframe: false
          },
        });
        return init;
      }
      catch (exception) {
        console.error("Keycloak initialization exception: " + JSON.stringify(exception, null, 2));
        return null;
      }
    }
    else {
      console.log("Keycloak config not stored.  No client available.");
      return null;
    }
  }
}

This works fine if the values for the realm/resource/etc are valid. But if they aren’t valid, or the KC server is down, I simply get a timeout error core.js:6498 ERROR {error: 'Timeout when waiting for 3rd party check iframe message.'}. The catch code with console.error() is never reached.

1reaction
Sazzelscommented, Jun 16, 2022

@gatos-cc i run keycloak-js and run into the same issue.

Using vuejs and trying to implement keycloak login. package.json

"keycloak-js": "^18.0.0"
// keycloakInitOptions 
const keycloakInitOptions = {
  onLoad: "login-required",
  checkLoginIframe: false,
};

const keycloak = new Keycloak(keycloakOptions); // *nothing special* URL, REALM, CLIENT_ID

// basic implementation (for testing)
keycloak
    .init(keycloakInitOptions)
    .then((res) => console.log(res))
    .catch((err) => console.error(err));

works when backend config is valid and server is reachable. As soon as i configure a non responding backend or i shutdown keycloak server the application is blocked an results in a 404 Page. Don’t let me start with when i configure a page which has nothing todo with keycloak (like: google.de, reddit.com, or else) then i get 404 pages from them (makes sense) but some kind of (is the backend a keycloak backend would be highly appriciated)

anyone got a working solution for this issue - i realy thinking of throwing keycloak out of this project

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Handling | keycloak-documentation
Keycloak has some error handling facilities for servlet based client adapters. When an error is encountered in authentication, Keycloak will call ...
Read more >
Keycloak "Unexpected error when handling authentication ...
After my own research, I now got it to work. The error message seems misleading because I don't have another identity provider configured...
Read more >
Server Administration Guide - Keycloak
To do so, start by creating new keys with a higher priority than the ... error-message: the key of the error message in...
Read more >
Unexpected error when handling authentication request to ...
java" and put it on the jar with the specific file in the META-INF.services folder. ... 4. login in the app that is...
Read more >
Saml identity provider error handling - Getting advice - Keycloak
Keycloak shows 'We are sorry…' page with 'An error occurred' text. Preferred functionality is to get directly back to application where started ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found