Alexander
Alexander

Reputation: 1378

Cannot extract error message from error.message

I have this line of code in my express app:

 catch (err) {
        res.status(500).send(err.message);
      }

when i console log the error I get this message:

name: 'TokenExpiredError',
message: 'jwt expired',

but when I recieve the error in my client using axios request like so:

catch (err) {
    console.log(err.message)

I get this : Request failed with status code 500

how can I access the original massage?

Upvotes: 0

Views: 98

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

You don't want to simply catch the error, a 500 error is just a 500 error (with it's own generic message).

You need to extract the message you send in the response from the response body. This is from the github issues pages for axios https://github.com/axios/axios/issues/960:

axios
.post('ajax/register/otp', this.registerData)
.then(function (response) {
       ...
})
.catch(function (error) {
      console.log(error.response);
 });

Upvotes: 1

Related Questions