title: Why are my error messages defined on a schema static method not showing up even though the error handling is apparently working?
body: <p>I've defined a static method on the schema that is defining the structure of User documents in my mongodb database. The method <code>.findByCredentials()</code> checks if a user-provided email and password match an existing user and (hashed) password in the database. In the event that the <code>user</code> cannot be found by the method, an error is thrown. The same is done if a user is found but <code>bcrypt.compare()</code> returns <code>false</code> (i.e., the provided password does not match what's stored in the DB).  I'm using Mongoose and Express.</p>
  
  <p><br>
  The problem I'm having is that the error messages I've defined are not being passed to my Express route - but the <code>.catch()</code> statement I have for handling errors IS firing when I pass bad credentials to the route (and therefore the middleware function).  I can update the <code>res.status()</code> and I can get a console log from the <code>.catch()</code> statement but I can't get the errors I defined in the middleware to show up.  Please forgive my... imprecise description - I'm relatively new to backend and servers so I'm still figuring out how to describe what I'm running into. </p>
  
  <p><br></p>
  
  <p>I've tried to define the <code>message</code> property on the Error by doing something like:</p>
  
  <pre><code>throw new Error({ message: "Some message goes here" })
  </code></pre>
  
  <p>and then modifying the <code>.catch()</code> in my Express route as follows:</p>
  
  <pre><code>.catch (error) {
    res.status(400)
    res.send(error.message)
  }
  </code></pre>
  
  <p>In its current state, the Express route has a console log that fires if an error exists - and the console log IS showing up in my console. But in Postman, the <code>res.send(error)</code> just shows an empty object. My Express application IS configured to parse JSON (<code>app.use(express.json())</code>) so I've also tried to play around with parsing the error but I've had no luck with that either. </p>
  
  <p>Express Route:</p>
  
  <pre><code>router.post('/users/login', async (req, res) =&gt; {
    const _email = req.body.email
    const _password = req.body.password
  
    try {
      const user = await User.findByCredentials(_email, _password)
      res.send(user)
    } catch (error) {
      res.status(400)
      if (error) {
        console.log("THERES An ERROR")  // THIS CONSOLE LOG IS FIRING
        res.send(error)
      }
    }
  })
  </code></pre>
  
  <p>Middleware (defined on a fairly stereotypical User schema)</p>
  
  <pre><code>serSchema.statics.findByCredentials = async function (email, password) {
    const user = await User.findOne({ email })
  
    if (!user) {
      throw new Error({ message: 'Unable to log in. Please check credentials and try again.' })
    }
  
    const isMatch = await bcrypt.compare(password, user.password)
  
    if (!isMatch) {
      throw new Error({ message: 'Unable to log in. Please check credentials and try again.' })
    }
  
    return user
  }
  </code></pre>
  
  <p>The desired outcome is just for me to access the error messages I defined in the static method. It's not essential to the way the application is running - this is definitely for my own edification. Again, as best I can tell, everything is working in terms of what the middleware is actually supposed to do - which is return a user document from the DB when a provided email address and password match what's stored in the database.  I just have no error messages and for the time being, I want to be able to see if the given email address or password is incorrect (though I understand that this might present a security concern in a real application).</p>