Last active
December 25, 2015 02:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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