0

From an Angular 4 app, I am calling REST services deployed on Tomcat. For Authentication, I am using Kerberos and it is working as expected for positive cases.

I am facing an issue in handling errors in Angular when Kerberos authentication fails (let's say due to user not existing in AD).

Here is my Angular code

this.restService.executeGET(url)
      .subscribe(
      (response: Response) => {
        let httpResponseStatus = response.status;
        console.log('httpResponseStatus: '+httpResponseStatus+', httpResponseAuthHeader: '+httpResponseAuthHeader)
        //Do something with the Response
      }
      ,
      (error) => {
        //Do something with the Error
        console.log('Authenication Failed' + error); 

      }
      )

 executeGET(url: string) {
    let reqHeaders = new Headers({ 'Content-Type': 'application/json' });
    reqHeaders.append('Accept', 'application/json');
    return this.http.get(url, { headers: reqHeaders });  
  }   

When Authentication succeeds and 200 is returned by the server, '(response: Response) => {}' gets executed as expected. When the response is 401 code with WWW-Authenticate: Negotiate header, neither the 'response' block nor the 'error' block gets executed and browser shows a 'Page can't be displayed' message.

When I look at IE 11 Network tab, it shows that Response has been returned as 401/Negotiate but for some reason it is evading the angular code.

Response Header 1

Response Header 2

It seems that browser is showing the 'Page can't be displayed' message even before the Angular code get a chance to load.

How can I ensure that some code block gets executed even when authentication fails, so that I can take appropriate action like redirecting user to login page.

T-Heron
  • 5,385
  • 7
  • 26
  • 52
kayasa
  • 2,055
  • 8
  • 37
  • 63
  • add service to handle an error like `401` like https://stackoverflow.com/questions/44108285/angular-4-custom-errorhandler-doesnt-recognize-custom-error – Yatin Patel Aug 11 '17 at 10:13

1 Answers1

1

Just add this check to the service http call

import 'rxjs/add/operator/catch';
.catch(e => {
            if (e.status === 401) {
                return Observable.throw('Not authorized');
            }
            // do other stuff
        });

And in the Component Subscribe

    this.your service.methodname()
        .subscribe(data => {
            // your data
        }, (err) => {
            if (err === 'Not Authorized') { 
// Do your stuff here
        });

And if you want a global 401 handler for your application service you can check this answer that uses http xhr in Angular Interceptor

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86