0

I have this code in client.add.component.ts and want to do error handling for Duplicate entry. I get 503 error in console currently.

this.clientsSvc.createClient(client).subscribe(x => this.router.navigate(['../'], {relativeTo: this.route}));

How to add the following line to the above code?

this.snackBar.open(`Duplicate Client Name`, null, {duration: 3000});
smiti
  • 31
  • 3

1 Answers1

1

I believe you want to show/handle error only when the current request fails for createClient

In that case to check status code in the error callback.

this.clientsSvc.createClient(client)
.subscribe(
   x => this.router.navigate(['../'], {relativeTo: this.route}),
   error => {
     if(error.status === 503) {
       this.snackBar.open(`Duplicate Client Name`, null, {duration: 3000});
     }

   }
);
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • I have the following code in client.service.ts file. createClient(post: Partial) { return this.http.post(this.api_url, post).pipe( tap(_ => this._updateClients.next(null)) ); } How to change this? – smiti Nov 09 '20 at 07:54
  • I think you always have the full response in the error callback, don't you ? I think the "observe" parameter only applies to the "success" callback... – Random Nov 09 '20 at 07:54
  • @smiti you don't have to change the code in `client.service.ts` change `this.clientsSvc.createClient(client).subscribe(x => this.router.navigate(['../'], {relativeTo: this.route}));` with the code that I mention in the answer – Pankaj Parkar Nov 09 '20 at 08:00
  • @Random My bad. You're right, updated the answer. Thanks for headup :) – Pankaj Parkar Nov 09 '20 at 08:02