Skip to content

Commit

Permalink
[fix] replicate node core behavior and throw an error if the user doe…
Browse files Browse the repository at this point in the history
…s not add their own error listener
  • Loading branch information
jcrugzz committed Jan 28, 2014
1 parent b606735 commit daad470
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/http-proxy/index.js
Expand Up @@ -68,7 +68,7 @@ function createRightProxy(type) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});

if(typeof this.emit === 'undefined' && !cbl) { throw new Error("You need to pass a callback to handle errors") }

for(var i=0; i < passes.length; i++) {
Expand Down Expand Up @@ -104,13 +104,22 @@ function ProxyServer(options) {
return ws[pass];
});

this.on('error', function(err) {
console.log(err);
});
this.on('error', this.onError.bind(this));

}

require('util').inherits(ProxyServer, EE3);

ProxyServer.prototype.onError = function (err) {
//
// Remark: Replicate node core behavior using EE3
// so we force people to handle their own errors
//
if(this.listeners('error').length === 1) {
throw err;
}
};

ProxyServer.prototype.listen = function(port, hostname) {
var self = this,
closure = function(req, res) { self.web(req, res); };
Expand Down

6 comments on commit daad470

@3rd-Eden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because crashing servers because your missing an error listener is the right thing to do™ ;)

@jcrugzz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3rd-Eden its a teaching mechanism ;)

@3rd-Eden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcrugzz there nothing wrong with teaching people this during development, but not in production ;-)

@glasser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it sure beats a console.log that you can't control!

@yawnt
Copy link
Contributor

@yawnt yawnt commented on daad470 Jan 28, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i'm not sure why i even wrote that.. @glasser i apologize for not paying enough attention in the past few days but college finals are coming up. please refer to @jcrugzz for the next couple of weeks :)

@glasser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries @yawnt, glad to see caronte out and happy to use it! Good luck on finals!

Please sign in to comment.