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

All exceptions are WebSocketError. More specific exceptions possible? #28

Closed
EriKWDev opened this issue Jul 12, 2021 · 8 comments · Fixed by #30
Closed

All exceptions are WebSocketError. More specific exceptions possible? #28

EriKWDev opened this issue Jul 12, 2021 · 8 comments · Fixed by #30

Comments

@EriKWDev
Copy link
Contributor

Hi!
I see that all exceptions being raised are just WebSocketError. It is hard to only except "Closed" exceptions in one way and other exceptions in their own ways.

Could we perhaps have something like WebSocketClosedError, WebSocketProtocolMismatchError, WebSocketHandshakeError and so on which could all be object of WebSocketError. This would ensure that scripts that already catch WebSocketError would still work (right?).

I might take a look tonight and see if I can submit a PR

@EriKWDev
Copy link
Contributor Author

I don't even know if WebsocketClosedError should be an error xP That one seems pretty integral for a websocket server that needs to keep track of connections.

Should it perhaps just be called WebsocketClosed? WebsocketClosedExcpetion?
Doesn't really matter but hey, semantics in programming sure is fun...

@treeform
Copy link
Owner

Basically any exception from the websocket is a close. Would you handle WebSocketProtocolMismatchError, WebSocketHandshakeError differently?

I think close should be an exception as the expected path for web socket to remain open forever.

@EriKWDev
Copy link
Contributor Author

EriKWDev commented Jul 14, 2021

While I see what you mean, I also think that a specific error as possible should be raised. I checked the test cases and a bunch of them except WebSocketError and assume that the thing that went wrong is in fact what the test is for, but with that mentality we might as well just raise Exception since "it wouldn't be handled differently anyway" xP The test for protocol doesn't even except WebSocketError just 'except:' and then compares the error message. This seems unacceptable? a WebSocketProtocolMismatchError would be a better solution here.

If the new exceptions are all of 'object of WebSocketError', old try-excepts would still work like they already do and the tests could become more specific. It wouldn't cost anything but would allow for cleaner code.

Some code duplication could go away as well since the WebSocketClosedError is raised in quite a few places. Perhaps a simple 'newWebSocketClosedError' with the correct message should be made?

@EriKWDev
Copy link
Contributor Author

EriKWDev commented Jul 14, 2021

I also beleive that this test (test_protocol_arr) is supposed to find a WebSocketProtocolMismatchError, but the actual WebSocketError being raised here is Failed to Upgrade (Possibly Connected to non-WebSocket url since the cb() responds with 404. Is this correct?

@treeform
Copy link
Owner

I would not mind WebSocketError hierarchy with WebsocketClosed, WebSocketProtocolMismatchError etc... I don't need it though, so I will not make this myself, but PRs welcome.

My fear is that we add this complexity but its not used by anyone in real production system and just creates a bigger bug surface.

@EriKWDev
Copy link
Contributor Author

EriKWDev commented Jul 14, 2021

Well, I believe that having more specific errors does the opposite of creating a bigger bug surface since it allows for better tests and debugging.

Users can still just except WebSocketError if they don't care and catch them all.

I'll make a PR since I do want this for myself anyway :)

Should the README be updated as well to contain the new Errors? Should the chatserv er example still use except: WebSocketErrorfor the closing sockets or should it specifically except WebSocketClosedError?

@treeform
Copy link
Owner

Please update the examples.

Don't worry about the README, we stopped using mddoc and use nimdocs now. I can change that part after your PR.

@treeform
Copy link
Owner

I updated the read me to our new style. Feel free to mess with it.

EriKWDev added a commit to EriKWDev/ws that referenced this issue Jul 14, 2021
Added more exceptions that are all of type 'object of WebSocketError'
so that it doens't break compatability with old try-except statements.

Also updated some of the tests to except these more specific errors
rather than the broad WebSocketError. Didn't change all though since
I was unsure about some of them such as test_protocol_arr.

closes treeform#28
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

Successfully merging a pull request may close this issue.

2 participants