Preface:
- Using Mongoose
- Using Bluebird and replacing mpromise inside of Mongoose
- The
req.helpers.consoleMessage
function seen bellow is a function with some simple logic in it that determines when to and not to display a certain level of detail based the existent of debug being turned on in the app configuration AND the non-null/undefined state of the objects being displayed. The entire messages gets stringified usingJSON
and returned to be displayed on the console.
Code:
Here is an example of some code showing these symptoms.
This is a delete
route for the:team
:comment
units in an API I'm working on. I have intentionally left the line var response = user.comments;
with an error in it referencing to a user
object when in fact it should be team
which should be returned by the calling function and passed into the Promise.then()
. This should cause a reference error as user is not defined.
var console = clim("(DELETE /api/v1/team/:team/comments/:comment):", logger);
// create a filters request for mongoose
var query = {};
// determine if the :team param is a username or an object id
req.helpers.validateObjectId(req.db, req.params.team) ? query._id = req.params.team : query.name = req.params.team;
if(req.helpers.validateObjectId(req.db, req.params.comment)) {
// looks good; create an update object
var update = { $pull: { comments: { _id: req.params.comment } } };
// find the comment using the query above and pull the comment id
req.models.Team.findOneAndUpdate(
query,
update,
{safe: true, new : true}
).then(function(team){
if(!team){
// create the response object
var response = {
success: false,
message: "Team not found"
};
// log request
console.info(req.helpers.consoleMessage(req, response, null));
// respond with an appropriate array
res.status(404).json(response);
}else{
// create the response object using the teams's comments
var response = user.comments;
// log request
console.info(req.helpers.consoleMessage(req, response, null));
// respond with the team comments array
res.status(200).json(response);
}
}).then(null, function(err){
// create the response
var response = { success: false, message: req.config.debug ? err: "An error has occur with your request; please try again" };
// log the errors
console.error(req.helpers.consoleMessage(req, response, err));
// or send a 500 internal server error
res.status(500).json(response);
});
}else{
// create the response
var response = { success: false, message: "Comment id is not a valid object id" };
// log the errors
console.info(req.helpers.consoleMessage(req, response, null));
// or send a 500 internal server error
res.status(500).json(response);
}
Symptom:
Calling this route will produce an error and cause the .catch()
to fire in an attempt to recover from the errored state however the err
object appears to be empty.
Ex. HTTP Response: { success: false, message: {} }
Ex. Console log (abridged for clarity) { req: {...}, res: {...}. err: {} }
Conclusion:
The err
object appears to be empty...