Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Forked from thom4parisot/gist:4663058
Last active December 11, 2015 21:38
Show Gist options
  • Save nfroidure/4663804 to your computer and use it in GitHub Desktop.
Save nfroidure/4663804 to your computer and use it in GitHub Desktop.
// Chainable API for URIBuilder
var uri = new URLBuilder(logErrorHandler)
.parse('bad_url_format')
.addQueryParam('param','value')
.setPort('443')
.toString();
function logErrorHandler(err, url_object){
if('parse_error'===err.type)
console.log(err.message);
return url_object;
}
// XXXXXXXXXXXXXX OR XXXXXXXXXXXXXXX
// Chainable API for URIBuilder
var uri = new URLBuilder()
.parse('bad_url_format')
.addQueryParam('param','value')
.setPort('443')
.toString();
if(uri===null)
{
console.log('error')
}
// Maybe the 2 solutions
// Or throw exception if i suppose the parsed string is never a user input ?
// Not sure it's good to suppose as well.
@naholyr
Copy link

naholyr commented Jan 29, 2013

Alternative:

var uri = new URLBuilder()
  .on('error', logErrorHandler)
  .parse('bad_url_format')
  .addQueryParam('param','value')
  .setPort('443')
  .toString();

No real need for EventEmitter (still, better if you have it) you can implement it manually or call it onError or whatever.

I still prefer exceptions, becasue as soon as input is invalid, the rest of the operations is senseless. Particularly: what will toString() return?

That's something I don't like in moment.js for example: give invalid (or unsufficient) input and you may have outputs like "NaN/NaN/NaN" I would really have preferred an exception here :s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment