Last active
March 30, 2016 23:55
-
-
Save aheuermann/5378cf3ee95fda7e0adff61ae46e2673 to your computer and use it in GitHub Desktop.
Not Found Error vs Return Null w/ handlers
This file contains 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
var facade = require('facade'), | |
handlers = require('handlers'); | |
app.use("/verification/:verificationId", funciton (req, res) { | |
facade.getUserForVerificationId(req.params.verificationId) | |
.then(handlers.successHandler, handlers.errorHandler); | |
}); |
This file contains 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
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; | |
}); | |
} |
This file contains 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
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); | |
} |
This file contains 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
var facade = require('facade'), | |
handlers = require('handlers'); | |
app.use("/verification/:verificationId", funciton (req, res) { | |
facade.getUserForVerificationId(req.params.verificationId) | |
.then(handlers.notFoundIfNullHandler, handler.errorHandler); | |
}); |
This file contains 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
module.exports = { | |
getUserForVerificationId: getUserForVerificationId | |
} | |
function getUserForVerificationId(verificationId) { | |
return db.filter({verificationId: verificationId}).one(); | |
} |
This file contains 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
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