Skip to content

Instantly share code, notes, and snippets.

@sporto
Created August 23, 2013 09:27
Show Gist options
  • Save sporto/6317306 to your computer and use it in GitHub Desktop.
Save sporto/6317306 to your computer and use it in GitHub Desktop.
Best practice on return values from module that handles http
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
});
@nicholasf
Copy link

If it's truly a HTTP wrapper then it should return a HTTP code (each time) otherwise push the protocol handler higher. :)

@sporto
Copy link
Author

sporto commented Aug 23, 2013

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?
});

@sidorares
Copy link

#3, 404 status in error object

@chrisdewar
Copy link

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