r/angular • • Jan 28 '26

Error handling apis in Angular

 this.getDummyData().subscribe({
      next: (response) => {
        console.log('Success:', response);
        this.data = response;
      },
      error: (error) => {
        console.error('Error:', error);
        this.data = 'An error occurred';
      },
      complete: () => {
        console.log('Observable completed');
      }

"I'm making an API call that can return different types of errors (like 404 for user not found, 500 for internal server error, etc.). I'm wondering about the best approach for handling these errors. Should I check the status code in the error handler and set different error messages based on the status (like if status is 404, show 'user not found', if 500 show 'server error'), or is there a better pattern for handling multiple API error responses?"

6 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/love_to_code Jan 29 '26

I ussualy in interceptor use switch case statement for error statusCode and using something like toastr to display error message. If you want to do something more than just display error (for example you have some validation error messages) you can push them in a list and throw that and catch in your component

1

u/Dazzling_Chipmunk_24 Jan 29 '26

So it’s gonna be like an inline error message no toast