0

I am trying to handle 422 error handling on my HTTP request. But somehow the error block doesn't run in the service using catchError operator. If there is no error the code is just working fine. I want to display error message on my page. But angular is only throwing error in console and not running the error block. Here is my http error response:

enter image description here

And here is the error I am getting only in console.

enter image description here

I want to display error message on my page. Here is my HTTP request.

 renderPlugin(id): Observable<any> {
 return this.http
      .get(`${environment.kbUrl}/${id}?_as_json=1&_nbp=1`, {
        responseType: "text",
        observe: "response"
      })
      .pipe(
        catchError((res: HttpErrorResponse) => {
          console.log(JSON.stringify(res));
          this.toastr.error("error", JSON.stringify(res));
          return throwError(JSON.stringify(res));
        })
      );
  }

And here is my method to subscribe to the it.

this.pluginsService.renderPlugin(this.currentId).subscribe(res =>{
    this.currentString = res.body;
    this.plugin = this.currentString.data.content;
    console.log(this.plugin);
      },
      err =>{
        this.plugin = err;
      });
5
  • why you need to use -> observe: "response"? for this is the only thing that can give problem, the rest of the code is "fine" Commented Jun 1, 2019 at 5:52
  • try removing the type from res to see if it works then and if its a differnet type of error Commented Jun 1, 2019 at 5:53
  • I have tried both suggestion but still not able to display any error. Not even console logging the error. Commented Jun 1, 2019 at 6:21
  • Is your http status code coming correctly as 422? You can check that in the developer tool. Commented Jun 1, 2019 at 9:09
  • Yes the status code is 422. I checked already in the developer tool. Commented Jun 1, 2019 at 12:46

2 Answers 2

2

Probably you have an interceptor that catches the error. If you need to handle errors in the interceptor, you may consider throwing them after your handling logic.

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing this code from your Http call:

  .pipe(
    catchError((res: HttpErrorResponse) => {
      console.log(JSON.stringify(res));
      this.toastr.error("error", JSON.stringify(res));
      return throwError(JSON.stringify(res));
    })
  )

And handle your error inside the error block of your subscribe method.

this.pluginsService.renderPlugin(this.currentId).subscribe(
    res => {
        this.currentString = res.body;
        this.plugin = this.currentString.data.content;
        console.log(this.plugin);
    },
    err => {
       // Handle the error here
       // like displaying toast using the error message received from server
       this.plugin = err;
    }
);

2 Comments

Still not working, I don't understand why its not even console.log(err) is not working.
I've created a stackblitz: stackblitz.com/edit/angular-wgl4km It is using a similar type of code as yours and it is working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.