title: Not able to respond to custom error message sent from express
body: <p>This problem annoys me, because I know it has something to do with me not understanding the issue properly - which makes it really hard to track down answers for, despite spending hours reading and trying different things.</p>
  
  <p>My question/problem is this, I am saving a user to a mongodb database when they signup, my schema doesn't allow for duplicate emails, and sends me back an error. I am able to console log the error in the terminal, but I am having problems sending it back to the client. Or I'm having a problem doing something with it, if it comes back, I'm not too sure where in those two steps I am losing access to the error message.</p>
  
  <p>Here is my POST route for saving the user:</p>
  
  <pre><code>router.post('/users', (req, res) =&gt; {
    let body = _.pick(req.body, ['email', 'password']);
    let user = new User(body);
  
    user.save().then(() =&gt; { // this all works and will save the user, if there are no errors
      return user.generateAuthToken();
    }).then((token) =&gt; {
      res.header('Authorization', `Bearer ${token}`).send(user);
    }).catch((err) =&gt; { // This is where my problem is
      console.log(err); // This will log the mongodb error here, about duplicate emails
      res.status(500).send(err); // I'm trying to send the mongodb error message back to the client to display it on the screen (I will handle making the message friendly to read, once I can get this to work)
    });
  });
  </code></pre>
  
  <p>So my catch is getting the mongo error, and then I try to respond with it, by sending it to the client.</p>
  
  <p>Here is my client side code:</p>
  
  <pre><code>axios({
    method: 'post',
    url: '/auth/users',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      email,
      password
    }
  }).then((res) =&gt; {
    console.log('this is the response', res);
    if (res.status === 200) {
      var authToken = res.headers.authorization.split(' ')[1];
      authenticateUser(authToken);
      this.props.history.replace('/dashboard');
    } // This all works fine for a signup with no errors
  }).catch((err) =&gt; {
    console.log('Signup error:', err);
    // I am expecting the above line of code to log the long Mongodb 
    // error message that I am sending back in my res.status(500).send(err)
    // catch call from the server, but instead all I am getting is
    // "Signup error: Error: Request failed with status code 500"
  });
  </code></pre>
  
  <p>Either I'm not sending the error correctly, or I'm not handling it correctly when it comes back, but I have no idea which it is or why.</p>
  
  <p>I can't even send back <code>res.status(500).send('some string here')</code> and access that string.</p>
  
  <p>Thanks</p>
  
  <h2>Update</h2>
  
  <p>So I just checked in postman, by sending a POST that could cause the error, and I am getting the correct response sent through.</p>
  
  <p>My server catch actually looks like this:</p>
  
  <pre><code>.catch((err) =&gt; {
    res.status(500).send({message: err.message});
  });
  </code></pre>
  
  <p>And the postman response body looks like this:</p>
  
  <pre><code>{
    "message": "E11000 duplicate key error collection: authBoilerplate.users index: email_1 dup key: { : \"email@example.com\" }"
  }
  </code></pre>
  
  <p>So I'm just not handling it correctly in my client side code, still at a loss though.</p>