An extendable KeyError for s3dol and more #6
Replies: 2 comments
|
Some inspiration code. class MyError(KeyError):
caught_errors = tuple()
@classmethod
def register(cls, error):
if error not in cls.caught_errors:
cls.caught_errors = cls.caught_errors + (error,)
MyError.register(AttributeError)
try:
list.foo
except MyError.caught_errors as e:
print(f"caught: {e}") |
|
Worked through this as part of the v1 redesign. Full write-up in First — the instinct behind this issue is more right than it looks
Measured, against botocore 1.40.53 and moto 5.2.2:
The reason is structural: HEAD has no response body, so there's no XML error document for botocore to model, so the modelled exception never fires. Any code that catches Worse, and closer to the real risk here: So the fix isn't just "catch more exception types" — it's that exception type is the wrong key to classify on. Why not the class-attribute registryThe proposed
What replaces itA translation table keyed on
And one seam, not a hierarchy: a single On "this may be better served in
|
Uh oh!
There was an error while loading. Please reload this page.
I see the last change that is meant to catch an error in getitem that should be considered a key error.
There's something for that in dol.Store already, but not sure how well it's set up. Should have a look.
About the current local patch...
I’m slightly wary about the fact you are only catching one exception there. Of course, we don’t want to waste time looking at all s3 (boto?) exceptions to see which should be considered key errors. But as it is, (1) you could miss some actual key errors and (2) evolving to accumulate other key errors we’ll observe in the future.
I have a few ideas to propose here.
In s3dol base we define a subclass of KeyError, call it S3KeyError. It has an attribute (the class does) whose value is a set (or list--but not tuple) of all s3 errors that should be considered key errors. It all has a method "register" that will modify that set.
The use should be obvious from there.
Even better would be if we define the S3KeyError to be a KeyError, as well as all the other ones we want. Then you can just catch that one error and also raise it (and since it's also a key error, it will trigger the base behavior.
In fact, perhaps that last solution is better. Only thing I don't know how to do yet. Cleanly is make it extendable. We need to reload the module to redefine reset of registered error classes. But maybe we don't really need a dynamic way to register errors quite yet. It's cleaner if we can because that means that the module whose context races a certain type of error is the one registering it. But for the sake of pragmatism, let's just do the minimum modification that makes it a lot better.
All reactions