Javatpoint Logo
Javatpoint Logo

Returning Error Message on the server in MEAN Stack

In our previous section, we successfully got the error dialog. It shows the standard error message for our sign-up error because we have the unique mongoose validator for the user route where we create a new user on the backend. If it detects an error, it will automatically send back the error, and we then just send back the normal error response.

Returning Error Message on the server in MEAN Stack

We will improve the errors by using the following way:

1) We will send back an error with our own error object where we add the message property and set the message Invalid Authentication Credentials.


Returning Error Message on the server in MEAN Stack

Now, if we go back to our angular and try to sign up with an already registered email, we will see the message which we exactly want to see.

Returning Error Message on the server in MEAN Stack

2) Let's go through the server-side and ensure that we are sending correct error messages in all cases. For logging in, we are also catching the error, and we are sending auth failed. Here, we want to show the "Invalid Authentication Credential!" message too.


Returning Error Message on the server in MEAN Stack

If we go back to our check-auth file, there we also send back a response with Auth failed if we identify that the user doesn't have a valid token. So, we want to update the message to say You Are Not Authenticated!.


Returning Error Message on the server in MEAN Stack

3) Now, we will go back to our posts route where we create a new post, we are not handling error cases. Here, we need to add a catch block after the then block. This will be executed whenever something does fail. We will set a response with a status code of 500 for something that went wrong on the server and add some JSON data where we can set up a message and set it with Creating A Post Failed!.


Returning Error Message on the server in MEAN Stack

4) Now, we will move on to the update path, where we are updating our posts. Here, we are already sending back a response if we don't have a technical error, which would go into a catch block, but if we simply don't find a post to update, we send back a "Not authorized!" error. We also want to catch the technical errors like database connection lost or anything else. Then we will simply return a custom response with a status code of 500 where we send back our JSON code and set the message property to Couldn't Update Post!.


Returning Error Message on the server in MEAN Stack

5) Now, we will move on to the getting post route. Here, we also need to catch the technical error, and we will do this in the same way as we have done with the update post. We only change the message to Fetching Posts Failed!.


Returning Error Message on the server in MEAN Stack

6) We will copy the same catch block code and add it to that method where we try to find a single post.

Returning Error Message on the server in MEAN Stack

7) Now, for deleting posts, we handled the case that we were not authorized or anything like that, and we will catch the technical error here too. So, we will add the catch block here for this like as:


Returning Error Message on the server in MEAN Stack

Download Complete Project(Error handling.zip)


Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA