-
Notifications
You must be signed in to change notification settings - Fork 46
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
Use Python built-in exception types #119
Comments
Yes, it's a good point. |
Yes, there are a few others. It's a bit awkward to keep Python 2 support because the exceptions there are different. Here are a few that I've noticed to start with:
Also, if GEMMI can open a file but there's a problem parsing it, maybe it should raise ValueError instead of RuntimeError? But that's a bit more debatable. @d-j-hatton has been using this too. Dan, have you noticed any other errors where a more specific type would be useful? |
That's more complex than I thought. For this, I suppose I'd need to check Maybe I could just raise |
Yes, just using |
Valid in Python 2, but it may be less useful in Python 3 as it would probably not be caught by an |
but why would you prefer writing except FileNotFoundError instead of a single except IOError? |
If you intend to handle one or more of those cases in your own code, rather than just passing the message on to the user. In our case, FileNotFoundError often just means the file hasn't appeared through NFS yet (or is still being copied by rsync, or being rewritten by another process, etc). So, if we know it's a FileNotFoundError we can just wait and try again, but if we get other types of IOError we probably have to stop and report a failure. As I said, I think IOError would be a useful improvement over RuntimeError, but using the "correct" Python 3 exception types would be even better. |
The only case where we need to catch the exception so far is when the file we want isn't there for one of the reasons mentioned by Colin. It may also be possible that we sometimes get partially written files which give you something like
which we also want to catch. At the moment we would be catching that with the So I suppose the ideal case is to have |
OK, I started working on it and will probably finish next week. BTW, if you are automating thing in DLS have a look at #116 |
I changed file reading errors and a couple of other ones to IOError. Regarding cif parsing errors, I think ideally we would have exception similar to json.JSONDecodeError, but creating custom exceptions with extra properties is not straightforward in pybind11, so in the end I used ValueError as you proposed. |
Great, that all sounds good. Thanks! |
It'd be very helpful if GEMMI could raise standard Python exception types when they're appropriate. The particular example we've found is trying to open a file that doesn't exist:
It would help us to write much more natural Python code if we could catch this as a standard FileNotFoundError rather than having to catch RuntimeError and look into the message to identify the problem.
The text was updated successfully, but these errors were encountered: