Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access exceptions thrown by Nestjs from frontend?

I am building backend API with Nestjs and I want to access exeptions from frontend

this is a simple example from login endpoint when user enters non-existent email :

const user = await this.userModel.findOne({ email });
    if (!user) {
      throw new NotFoundException('email does not exist  .');
     }

now when I send request from my Nextjs application like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 await axios
        .post(`http://127.0.0.1:5000/user/login`, {
          email: values.email,
          password: values.password,
        })
        .then((res) => {
          console.log(res);
        })
        .catch((error) => {
          console.log("Login catched an error : ", error);          
        });

I get the NotFoundException as error:

enter image description here

now I want to get the status of this error (404 in this case),is it possible ?

>Solution :

It’s an AxiosError, so you should be able to access error.response.status as shown in Axios’s documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading