Skip to content

Instantly share code, notes, and snippets.

@aheuermann
Last active March 30, 2016 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheuermann/5378cf3ee95fda7e0adff61ae46e2673 to your computer and use it in GitHub Desktop.
Save aheuermann/5378cf3ee95fda7e0adff61ae46e2673 to your computer and use it in GitHub Desktop.
Not Found Error vs Return Null w/ handlers
var facade = require('facade'),
handlers = require('handlers');
app.use("/verification/:verificationId", funciton (req, res) {
facade.getUserForVerificationId(req.params.verificationId)
.then(handlers.successHandler, handlers.errorHandler);
});
module.exports = {
getUserForVerificationId: getUserForVerificationId
}
function getUserForVerificationId(verificationId) {
return db.filter({verificationId: verificationId}).one()
.then(function (user) {
if (!user) {
return Promise.rejected(new NotFoundError("User not found by verification:" + verificationId));
}
return user;
});
}
module.exports = {
errorHandler: errorHandler,
successHandler: successHandler
}
function errorHandler(err, res) {
if (err instanceof NotFoundError) {
return res.send(404);
}
return res.send(500);
}
function successhandler(result, res) {
res.send(200, res);
}
var facade = require('facade'),
handlers = require('handlers');
app.use("/verification/:verificationId", funciton (req, res) {
facade.getUserForVerificationId(req.params.verificationId)
.then(handlers.notFoundIfNullHandler, handler.errorHandler);
});
module.exports = {
getUserForVerificationId: getUserForVerificationId
}
function getUserForVerificationId(verificationId) {
return db.filter({verificationId: verificationId}).one();
}
module.exports = {
getResponseHandler: getResponseHandler,
notFoundIfNullHandler: notFoundIfNullHandler
}
function notFoundIfNullHandler(result, res) {
if (result == null) {
return res.send(404);
}
return res.send(200, result);
}
function errorHandler(err, res) {
res.send(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment