Skip to content
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

Feature request: Improve error handling (error thrown from open() should contain the actual error, e.g. ECONNREFUSED) #47

Open
titanism opened this issue Nov 1, 2023 · 1 comment

Comments

@titanism
Copy link

titanism commented Nov 1, 2023

Right now the only way to detect if this library throws an error on connection (as opposed to something else) is to check for the string err.message.startsWith('WebSocket closed with reason:').

For example this error is thrown on an unsuccessful await wsp.open() call:

RWS> addListeners
RWS> error event connect ECONNREFUSED ::1:3456
Error: WebSocket closed with reason:  (1000).
    at WebSocketAsPromised._handleClose (/Users/user/Projects/web/node_modules/.pnpm/websocket-as-promised@2.0.1/node_modules/websocket-as-promised/src/index.js:408:19)
    at listener (/Users/user/Projects/web/node_modules/.pnpm/websocket-as-promised@2.0.1/node_modules/websocket-as-promised/src/index.js:340:64)
    at ReconnectingWebSocket._callEventListener (/Users/user/Projects/web/node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:557:13)
    at /Users/user/Projects/web/node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:197:79
    at Array.forEach (<anonymous>)
    at ReconnectingWebSocket._handleClose (/Users/user/Projects/web/node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:197:36)
    at ReconnectingWebSocket._disconnect (/Users/user/Projects/web/node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:540:18)
    at ReconnectingWebSocket._handleError (/Users/user/Projects/web/node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:180:19)
    at callListener (/Users/user/Projects/web/node_modules/.pnpm/ws@8.14.2_nn4cfacdngcg7bs47xs3zpla7u/node_modules/ws/lib/event-target.js:290:14)
    at WebSocket.onError (/Users/user/Projects/web/node_modules/.pnpm/ws@8.14.2_nn4cfacdngcg7bs47xs3zpla7u/node_modules/ws/lib/event-target.js:230:9)
    at WebSocket.emit (node:events:513:28)
    at emitErrorAndClose (/Users/user/Projects/web/node_modules/.pnpm/ws@8.14.2_nn4cfacdngcg7bs47xs3zpla7u/node_modules/ws/lib/websocket.js:1016:13)
    at ClientRequest.<anonymous> (/Users/user/Projects/web/node_modules/.pnpm/ws@8.14.2_nn4cfacdngcg7bs47xs3zpla7u/node_modules/ws/lib/websocket.js:864:5)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:502:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Having access to the raw error would be useful for error handling purposes (e.g. being able to see err.code is ECONNREFUSED.

Additionally, the err.reason property can be empty, and as you can see from the above example it has extra whitespace added to the error string (which isn't dev-friendly). This will require a rewrite to _handleClose and _handleError handlers most likely (?)

Reference:

const error = new Error(`WebSocket closed with reason: ${event.reason} (${event.code}).`);

@vitalets
Copy link
Owner

vitalets commented Nov 4, 2023

Good point, I agree.
Would appreciate a PR for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants