0

I am sending an HTTP request from angular to a node/express API. How do I get the actual error message I send from the node/express API

I am handling my exceptions well when they are thrown in the API but the moment I try to read the message in Angular I only get the name of the type of error I throw. For example, if I throw a 409 the error received by angular is just "Conflict" and does not contain the details I send. Please look at my code below.

I am sending my request as below

register(user: UserAccount) {
    return this.http
      .post(`${config.apiUrl}/users/register`, user)
      .pipe(
        map((res: HttpResponse<Response>) => {
          return res;
        }))
      .pipe(catchError(err => this.errorHandler.handleError(err)));
  }

My handle error is as below:

handleError(error: HttpErrorResponse) {
    console.log(error);
    if (error) {
      let errMessage = '';
      try {
        errMessage = error.message;
      } catch (error) {
        errMessage = error.statusText;
      }
      return throwError(errMessage || error || 'Server error');
    }
    return throwError(error.error || error || 'Server error');
  }

This how I am throwing my error when I occurs in my Node/Express API

 registerUser (req, res) {
    debug(chalk.blue(`*** insert user`))
    userRepo
      .create(req.body)
      .then(user => {
        debug(chalk.green(`*** Insert User ok!`))
        res.status(200).json({
          status: true,
          error: null,
          user: user
        })
      })
      .catch(err => {
        debug(chalk.red(`*** insertUser error: ${util.inspect(err)}`))
        if (err['type'] && err['type'] === '409') {
          res.status(409).json({
            status: false,
            error: err['message'],
            user: null
          })
        } else {
          res.status(400).json({
            status: false,
            error: err,
            user: null
          })
        }
      })
  }

I want to be able to receive the json object with the information about the error but all I am getting when I access the error item is, for example, in the case of raising a 409, I only get 'Conflict'

1 Answer 1

2

The reason for this is that when you catch the error and the status is a 409, you return `err['message'] instead of 'err'.

So instead of:

res.status(409).json({
            status: false,
            error: err['message'],
            user: null
          })

You should return:

res.status(409).json({
                status: false,
                error: err,
                user: null
              })

This is actually what you do in the case of a 400 error!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for you response. Its not actually the solution but you answer led me to the right solution. I adjusted you answer to match the working solution
Just out of curiosity, what was the problem then? :)
the problem was I was returning the object: res.status(409).json({ status: false, error: err, user: null}) instead of just doing res.status(409).json(err). Accept my edits to update the answer

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.