Skip to content

Instantly share code, notes, and snippets.

@focusaurus

focusaurus/gist:6905035

Last active Dec 25, 2015
Embed
What would you like to do?
helper function to reduce mongoose/REST boilerplate. Could also monkey patch into OutgoingMessage.prototype perhaps.
function oopsAPI(res, error, model) {
if (error) {
logger.error('Unexpected mongoose error during API query', error);
res.status(500).send(error);
return true;
}
if (!model) {
res.status(404).send();
return true;
}
return false;
}
//to use in an API route handler
someQuery.exec(function (error, model) {
if (oopsAPI(res, error, model)) { return; }
//success code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.