New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit error instead of throw #1558
Conversation
I actually like the idea of emitting all errors to the |
@nylen In our code base we have this wrapper used only at the end of the So we can have this code but it looks ugly, and in some cases it might cause issues I think. defer(self.emit.bind(self, 'error', new Error('...')))
// instead of
self.emit('error', new Error('...')) |
When I do error events before they can be listened in my code (i.e., constructors), I use Either all events/errors are emitted asynchronously, or they're thrown synchronously. Do not mix synchronous and asynchronous or you will release Zalgo. If you do decide to throw/emit synchronously (where you can't |
I see the value in both behaviors, but I prefer what we have currently to this. The thinking is that errors that occur on initialization are caused by bad input / are programmer errors and should get more exposure, while async errors related to redirects and responses are expected errors (something can always go wrong). |
I was trying to do something like that (naively):
And get 'got ya' called on invalid URL for example. You can export a module which behaves like If you are okay with one bit ugly copy operations of the and it can be used as follows:
which produces:
https://gist.github.com/saper/67d2bd6a6cd81ee9773a I think this is a pretty crazy idea but may be we can achieve something useful this way. |
You could work around this by emitting any initialization errors before adding the default
This feels a little too "magic" to me. My other concern with making changes here - are we creating situations where we return a broken, half-initialized object? |
On Mon, 4 May 2015, James Nylen wrote:
Attaching a handler to an initialized Request obviously does not work... Given that we often face situation
I think yes, but that's what we do now anyway:
This is the situation we have right now I think with the case: https://gist.github.com/saper/8ddf5f180f848379bc47
where we get with the current master:
We never return an object straight from the constructor |
Of course this is expected. We are simply doing
There is no way such a handler would magically be bound to the
I just had a look at node's http module and they solve it nicely: |
It is expected for single error event to pop up on various places through the entire app, and multiple times - that's some weird expectations TBH :) |
I have to agree :) well, I never really expected we will go this way - it was just a funny exercise in JavaScript. But I agree |
Ok, so this tiny PR makes the error behavior consistent only when you have a callback. I'm glad we raised the awareness about the error related issue, but like I said a few comments above, I'm not sure about the best way to wrap the emit statements, so if there aren't any other suggestions I'm going to merge this PR shortly. |
Related to this #1318 (comment) onward
Breaking change
Any code that previously relied on catching the errors in the
catch
block will now receive them in thecallback
as errors if acallback
is specifiedNo callback during initialization
This repository still have two error cases that throws in index.js But if the callback is missing, any error will be thrown anyway
After successful initialization
One possible way to receive the errors during initialization in the
on('error')
event handler, is to use this hackBut it's unreliable and I'm not going to use it.
// cc @mikeal @nylen @FredKSchott @saper