title: How to Throw custom Errors?
body: <p>I am trying to use <code>passport-jwt</code> strategy for authentication.</p>
  <p>Here is my code :-</p>
  <pre class="lang-js prettyprint-override"><code>router.post('/register', async (req, res) =&gt; {
      const { username, email } = req.body;
      try {
          const user = await User.findOne({ username });
          if (user) {
              throw new Error('User with same username already exists !!');
          }
          const newUser = new User({
              username,
              email
          })
          const salt = await bcrypt.genSalt(10);
          newUser.password = await bcrypt.hash(req.body.password, salt);
          const savedUser = await newUser.save();
          res.json({
              status: 200,
              'Content-Type': 'Application/Json',
              'message': `You have successfully regirstered yourself, ${savedUser.username}!`
          })
      } catch (err) {
          err.statusCode = 500;
          console.log(err.message);
          res.header({
              status: '200',
              'Content-Type': 'Application/Json',
          });
          res.json(err);
      }
  });
  </code></pre>
  <p>Now this route is working just fine, it's doing all the things till now. The only problem is, When i find an existing user, I want to throw a new error with a custom message. Javascript has this <code>Error</code> class which I can use to throw these errors.</p>
  <p>The problem occurs when it catches error. When I <code>console.log(err.message)</code>, I can see my custom message perfectly. But the <code>err</code> object that I am returning in response via <code>res.json(err)</code> does not have any <code>message</code> but only <code>statusCode</code>.</p>
  <p>I want to know why this is happening and what's the solution for this ? Right now, I am doing this by doing something like this :-</p>
  <pre><code>res.json({
      statusCode: 500,
      message : err.message
  });
  </code></pre>
  <p>But I would like to return the <code>err</code> object with both <code>statusCode</code> and <code>message</code> fields populated.</p>