Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 HTTP - How to understand that the backend server is down

I am developing a front end which consumes JSON services provided by a server.

I happily use HTTP of Angular2 and I can catch errors via .catch() operator.

If I find a problem related to a specific service (e.g. the service is not defined by the server) the catch() operator receives a Response with status 404 and I can easily manage the situation.

On the other hand, if it is the server that is completely down, the catch() operator receives a Response with status code 200and no specific sign or text related to the cause of the problem (which is that the whole server is down). On the console I see that angular (http.dev.js) writes a message net::ERR_CONNECTION_REFUSED but I do not know how to do something similar (i.e. understand what is happening and react appropriately) from within my code.

Any help would be appreciated.

like image 617
Picci Avatar asked Feb 24 '16 12:02

Picci


People also ask

How http works in angular?

Most front-end applications need to communicate with a server over the HTTP protocol, to download or upload data and access other back-end services. Angular provides a client HTTP API for Angular applications, the HttpClient service class in @angular/common/http .


2 Answers

If you would like to handle this event globally in your application I recommend using slightly modified Nicolas Henneaux's answer https://stackoverflow.com/a/37028266/1549135

Basically you can check for error.status === 0 which happens when the net::ERR_CONNECTION_REFUSED error occurs.

The complete module file:

import { Request, XHRBackend, BrowserXhr, ResponseOptions, XSRFStrategy, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

export class AuthenticationConnectionBackend extends XHRBackend {

  constructor(_browserXhr: BrowserXhr, _baseResponseOptions: ResponseOptions, _xsrfStrategy: XSRFStrategy) {
    super(_browserXhr, _baseResponseOptions, _xsrfStrategy);
  }

  createConnection(request: Request) {
    let xhrConnection = super.createConnection(request);
    xhrConnection.response = xhrConnection.response.catch((error: Response) => {
      if (error.status === 0){
        console.log("Server is down...")
      }
      ...
      return Observable.throw(error);
    });
    return xhrConnection;
  }

}

Module file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule, XHRBackend } from '@angular/http';
import { AppComponent } from './app.component';
import { AuthenticationConnectionBackend } from './authenticated-connection.backend';

@NgModule({
    bootstrap: [AppComponent],
    declarations: [
        AppComponent,
    ],
    entryComponents: [AppComponent],
    imports: [
        BrowserModule,
        CommonModule,
        HttpModule,
    ],
    providers: [
        { provide: XHRBackend, useClass: AuthenticationConnectionBackend },
    ],
})
export class AppModule {
}
like image 169
Atais Avatar answered Sep 19 '22 18:09

Atais


I have the same problem while using angular2.0.0-beta.15

It seems like this is a bug. You get http status 200 and this is not correct:

https://github.com/angular/http/issues/54

like image 28
Herman Fransen Avatar answered Sep 19 '22 18:09

Herman Fransen