Created
August 23, 2013 09:27
-
-
Save sporto/6317306 to your computer and use it in GitHub Desktop.
Best practice on return values from module that handles http
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 myModule = require('myModule'); | |
myModule.fetch('some/url', function (err, val) { | |
//if 'some/url' cannot be found what should myModule return? | |
//some options I can think of | |
//#1 | |
//err => null | |
//val => 404 | |
//#2 | |
//err => 404 | |
//val => null | |
//#3 | |
//err => A proper JS Error | |
//val => null | |
}); |
To be more specific:
var youTubeInfo = require('youTubeInfo);
youTubeInfo.fetch('http://www.youtube.com/watch?v=hqAyiqUs93c', function (err, val) {
// normally val will be an object like:
// {title: 'xyx', ...}
// but if the url is wrong, what should the callback get?
});
#3, 404 status in error object
if you are looking for a 1, 2, or 3... i'm inclined to say 3, because of the fs api... even if the filesystem (read: http) is working properly, a missing file results in an error.
if you are looking for "best practice" however, the http.get api returns an object that you can then .on('error', function (err) {});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If it's truly a HTTP wrapper then it should return a HTTP code (each time) otherwise push the protocol handler higher. :)