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
Websocket errors can be emitted as ErrorEvents instead of Errors #2493
Comments
I'd be more interested in other examples you mentioned? |
I don't think I've encountered the issue with any non- The reason I expect it to happen with other websocket errors is that they all seem to be handled the same way inside discord.js/src/client/websocket/WebSocketConnection.js Lines 364 to 375 in e5bd6ec
ws.onerror property:
But both Mozilla's web API documentation and ws's documentation seem to suggest or state that, when bound this way, the callback receives an event, not the error itself (and so the client emits an event, not the error itself, hence the issue). I'm not super confident submitting a pull request on this – partly because I've never contributed here before and partly because I don't know how the individual websocket libraries work that well – but I could try? |
This will be fixed in master once #3192 gets merged, and Client#shardError will now consistently emit an Error object. This difference was mostly due to uWs emitting an Error, while ws emits a ErrorEvent-like structure, which discord.js didn't handle. |
Please describe the problem you are having in as much detail as possible:
The parameter emitted by Client on the
error
event is sometimes an ErrorEvent, not an Error. This happens, in particular, when the websocket connection is broken by putting my laptop to sleep.The documentation for
Client#error
claims that the parameter will be of type Error: https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-error. At least when usingws
, due to the way discord.js binds to it, the event handler is given a parameter of type ErrorEvent when the websocket connection is broken. (This ErrorEvent is not exactly https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent, but instead a recreation in ws: https://github.com/websockets/ws/blob/d390dc5b34c269669d5fa4450cd394d957055296/lib/event-target.js#L87.)It is easy to get the error from this ErrorEvent – simply by using
errorEvent.error
– but it did cause me a bit of trouble, and means that simply logging errors that the client outputs won't always give a useful stack trace. (In a slightly different case, where I was handling these errors, it meant that I got a very confusing output for the event listener onws
's side, and no details of the error at all.)Include a reproducible code sample here, if possible:
Run this code, with a token as a parameter, and then somehow disconnect your internet connection to prompt a websocket error. On my Windows 10 laptop, when I enable flight mode, I get the following output:
Further details:
The text was updated successfully, but these errors were encountered: