EduBw
EduBw

Reputation: 942

Control errors the Angular from Server

I am controlling in Angular when the Server insert good o my Server can't insert, , but If my server is down I can't receive message:

  this.service.addConfig(url, newConfig).subscribe(param => {
                    this.confirmInsert = Boolean(param[0]);
                    this.messageInsert = param[1];
                    if ( this.confirmInsert) {
                        this.successInsert = true;
                        this.openModalAdd = false;
                        this.cleanAddForm();
                        this.fetchData();
                    } else {
                        this.errorInsert = true;
                    }

Angular-service:

 addConfig(url, newConfig) {
    return this.http.post(url, newConfig , { responseType: 'text'});
   }

But If I stop my server and execute my app, My modal doesn't close and I can't show modal-error

I get in console,html:

POST http://localhost:8080/create 0 ()
core.js:1449 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}

Then How Can I show my modal-error ?

Upvotes: 1

Views: 90

Answers (1)

JanRecker
JanRecker

Reputation: 1847

The .subscribe() Operator has three paramaters

.subscribe(
   onNext => // Do some magic with the new data
   onError => // Do some dark magic when the world falls apart
   onCompletion => // Enjoy the day, because the stream ended
)

you are currently only using "onNext". So, if the whole stream goes rogue, you do not react to that.

No reaction of your server (a timeout) is a "rogue" stream.

Perhaps try something like

this.service.addConfig(url, newConfig).subscribe(
param => {
    this.confirmInsert = Boolean(param[0]);
    this.messageInsert = param[1];
    if ( this.confirmInsert) {
        this.successInsert = true;
        this.openModalAdd = false;
        this.cleanAddForm();
        this.fetchData();
    } else {
        this.errorInsert = true;
    },
error => this.errorInsert = true;
)

warm regards

Upvotes: 1

Related Questions