0

Currently developing a site with the Angular framework I found myself facing a situation that confuses me and makes me wonder.

Indeed, like the majority of websites / applications in 2020, the user has the possibility to identify himself to his account in order to access his personal information.

Having already developed a Flutter application to train myself, I had set up an identification system via login/password using calls to an API.

This call is visible in the image below. As you can see in this picture, I retrieve the API response thanks to an await on the POST request.

Then I made a switch on the statusCode of my answer.

Flutter api call

And this is where my question comes. I thought I'd take the same thought for my Angular site, but a "problem" arises.

When I set up this "method" under Angular, if I enter bad user information, I don't really get an answer, but the browser tells me that there was an error.

You will see the different scenarios in the two captures below.

enter image description here

My question is finally "simple":

Why can I switch on Flutter response's statuCode & not in Angular ? more precisely, why are the 400 answers considered as errors in Angular and not in Flutter?

Simply a difference in implementation between the 2 frameworks?

1 Answer 1

1

It's a difference in the frameworks. However you can still do what you are after with Angular using the observable's pipe and the catchError operator. (This assumes you are using a recent version of Angular)

this.http.post(..).pipe(
    catchError((err) => {
        switch(err.status) {
            case 400: {
               // Do something
               break;
            }
            case 500: {
               // Do something
               break;
            }
            default: { 
               //Do something 
               break; 
            } 
        }
    }).toPromise();
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks for your answer & for the trick with the pipe :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.