3

I am developing a basic Node app and I am facing a problem that I think is not new but I don't understand which is the proper way to solve. I need to handle those errors that are not generated by code, network or database but needed by the logic of the application. As an example I use the already registered user but the same handling will be needed in different case in the application.

I start from the method in login component:

register () {
  API
    .register(this.credentials)
    .then(
      data => {
        this.user = data;
      },
      error => {
        this.error = error;
      })
    .catch(error => {
      this.error = error;
    });
},

The API is:

register (credentials) {
 return Axios
  .post('/auth/register',credentials)
  .then(response => {
    return response.data;
  })
},

The Backend is:

router.post('/auth/register',(req,res) => {
User.findOne({where:{username:req.body.username}})
  .then(user => {
    if (!user) {
      User
        .create(req.body)
        .then(user => {
          res.send(user);
        })
    } else {
      throw 'Username already exists';
    }
  });
});

What I expect is that the error

throw 'Username already exists';

(but it can be No data found for your search) is passed back to the component that catch it and show the error instead the registerd user. I try adding the catch after every then or using res.send({error:...}) instead of throw but in this way the component shows the error message as user object.

1 Answer 1

4

Use HTTP Status Codes to propagate errors through HTTP:

Backend:

router.post('/auth/register',(req,res) => {
console.log('backend register 3',req.body);
User.findOne({where:{username:req.body.username}})
  .then(user => {
    if (!user) {
      User
        .create(req.body)
        .then(user => {
          console.log('backend register 4',req.body);
          res.send(user);
        })
    } else {
      res.status(409).send('User already exists')
    }
  });
});

Fronted:

register (credentials) {
 return Axios
  .post('/auth/register',credentials)
  .then(response => {
    return response.data;
  })
  .catch(err => {
    processError(err);
  })
},
Sign up to request clarification or add additional context in comments.

4 Comments

I try this way also but status(409) is received by Axios not in then section but in catch section resulting in the component as a standard error message with status code and without my custom message.
The best solution for this is to use axios response interceptor, as you are already using axios. And by using that you can handle all the errors from a single place.
The axios response interceptor logs the same error as .catch method. It just says "Request failed with status code 409" not the custom error passed from Express :(
Then you are correctly receiving a 409 error from your express. Watch out, error code and custom message often are two different fields. Look into your err object to find the message.

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.