Skip to content

Instantly share code, notes, and snippets.

@focusaurus
Last active December 25, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save focusaurus/6905035 to your computer and use it in GitHub Desktop.
Save focusaurus/6905035 to your computer and use it in GitHub Desktop.
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