KeepCalmAndCarryOn
KeepCalmAndCarryOn

Reputation: 9075

How do I unpack this error with Mongoose and Promisify?

I have this in Mongoose and Promisify

This is the pre save conditions - I check for a pause (which times out) and then add one if all okay

//check if there is a pause for this id
HonkSchema.pre('save', function(next) {
  var query  = Pause.where({ id: this.id }).findOneAsync()
    .then(function(res){
        if(res) {

          var e = new Error('Not 5 Minutes')
          next(e)
        }//else {
          next()
        //}
    })
    .catch(function(err){
      console.log(err)
      var err = new Error(err);
      next(err);
    })
});

//Add pause
HonkSchema.pre('save', function(next) {
  Pause.createAsync({id: this.id})
    .then(function(res){
      next()
    })
    .catch(function(err){
      console.log(err)
      var err = new Error(err);
      next(err);
    })
});

I create a new one like this

// Creates a new Honk in the DB
export function create(req, res) {
  Honk.createAsync(req.body)
    .then(responseWithResult(res, 201))
    .catch(handleError(res, 412));
}

And handleError does this

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    console.log(err)
    res.status(statusCode).send(JSON.stringify(err));
  };
}

The log message above gives this

{ [Error: Not 5 Minutes] cause: [Error: Not 5 Minutes], isOperational: true }

But the error message to the client is this

{"cause":{},"isOperational":true}

So my question is, how do I get a meaningful message into the client?

Upvotes: 1

Views: 614

Answers (2)

Vasim Shaikh
Vasim Shaikh

Reputation: 4532

I Found That error in findOne query find more then one result means it is operational error.

Upvotes: 0

robertklep
robertklep

Reputation: 203504

You can use something like this:

return res.status(statusCode).send({ error : err.message });

Upvotes: 1

Related Questions