Handle 401 error

Hi, I try to used a JSON API back-end secured by keycloak. This back-end reponse with a 401 www-autenticate.

The problem is that i’m unable to retrieve the status code of the error. I’ve tried to overwrite the adapter.handleResponse(). But this does not work. The application route error object does not contains the status code. Any idea how to handle this case?

Hi @scandinave! I’ve used the error hook in the route to handle such things. See the docs for more information, but what I’ve done for this & other types of errors:

handleResponse in app/adapters/application.js:

export const UnauthenticatedError = AdapterError.extend({message: 'Unauthenticated'});

...

handleResponse(status, headers, _payload, requestData) {
  if(status === 401) { return new UnauthenticatedError() }
  /* other cases here */

  return super(...arguments);
}

and then in app/routes/application.js:

import { UnathenticatedError } from '../adapters/application';

...

actions: {
  error() {
    if(error instanceOf UnauthenticatedError) {
      this.transitionTo('login');
    } else if (/* other cases */) ....
  }
}}