-
Notifications
You must be signed in to change notification settings - Fork 304
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
FR: Subclassing exceptions to make them easier to catch #527
Comments
If possible, I'd like just a little feedback - if you are not directly opposed to the idea I would start working on a PR :-) |
Just one one about
since #522 this one usually also has description and can be further classified into above mentioned categories. |
I am not against this in principle, but I tend to not implement specific subclass errors until I actually need them, and I have not needed them as of yet in Bleak. @mihtjel you are free to try. If you can improve Bleak by doing this, I am all for it. The hassle is that there are four backends in three different OS:es to implement and test, so it is a daunting task... |
Thanks for the reply - I'll try to do it for the most obvious ones that I have wanted to catch and treat separately myself as a start at least. :-) |
"Device not found" is an "expected exception" for applications where the target device comes and goes and the application periodically checks for the device's return. Current check (based on what has been observed, not searching the code) is
In the same type of application, I can see it being desirable to "blanket catch" the class of errors related to trying an operation on a device that was connected, but no longer is, rather than checking for connectivity prior to every operation. (Not presently implemented in my code.) |
There should be more specific exceptions thrown, I agree. But in you application, it must be possible to narrow the scopes of your try-catch blocks to catch what happens in each BLE call, no? You should not have to parse the text to handle the error programmatically? |
The try-catch is very narrow and it is likely than a |
How can we proceed here? For my use case also "device not found" is a expected exception for unpair operations. |
"device not found" seems like a good starting point since it has been mentioned several times here. What would the specs be? My initial thoughts are that it should mean "the OS bluetooth stack has never seen this device or it was removed and forgotten". So for BlueZ, this would mean that there is no D-Bus object that corresponds to this device. On macOS, it would mean that |
For a "not connected" error, we could save some work on the implementation by waiting for #982 to be merged. Then it only has to be implemented in the front end instead of each individual backend. Although this one is a bit tricky since a device can be disconnected during an operation in addition to not being connected when the operation is connected. Is that the same error or a different error? |
That's exactly what I would expect from this exception.
Is is theoretically possible to pair with an device without being connected? Pairing requires communication so I had expected calling For the WinRT backend: Besides So for now we should be fine with raising This might be still a problem for devices with resolvable private addresses but as we don't change anything to the device discovery we won't make it worse for them. What about naming and data? |
I was wondering the same thing 😉. #309 (comment) So I'll answer the questions there so we don't get too off topic here.
This may be true for Window Store apps, but I'm not sure that it is true for "desktop" apps.
👍 Would the identifier be the |
I created a fist draft for further discussion: |
Description
I've been writing a piece of code that's getting .. more complicated than it probably should, I guess. But I have gotten a little annoyed by one thing in particular: The difficulty in handling the exceptions I get from Bleak:
Logger.error(f"Bleak error encountered: {e} ({e.__class__.__name__})")
gives me lines in my log like:
etc.
The first one is maybe difficult to do anything about, okay. But I have to do a manual parsing of the contents in my
except BleakError as e:
handler of the second two - and I think it would be more obvious if they were for example BleakDeviceNotFoundError and BleakNotConnectedError*.So, the suggestion: Make some exception classes for the expected types of returns from the library, and use BleakError for the rest.
(Obviously I'd be willing to contribute the code as well, but I won't start working on it if it's against a project principle.)
For example:
*: Or indeed BleakDeviceNotFoundException, as it is really not considered an error in the classical sense - a device not being found? But that's a different matter, and an issue with the Python naming conventions.
The text was updated successfully, but these errors were encountered: