From 3f5154fb50f1d44023e2d9e3e439e60306929b6a Mon Sep 17 00:00:00 2001 From: Ben Kehoe Date: Wed, 2 Nov 2022 18:40:23 -0500 Subject: [PATCH] v2.6: Add errors.ALL --- CHANGELOG.md | 3 +++ README.md | 4 ++++ aws_error_utils/aws_error_utils.py | 4 +++- pyproject.toml | 2 +- test_aws_error_utils.py | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f21daf5..fa97959 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ `aws-error-utils` uses [monotonic versioning](blog.appliedcompscilab.com/monotonic_versioning_manifesto/). +## v2.6 +* Add `errors.ALL` to match all `ClientError`s. + ## v2.5 * Fix type annotations. diff --git a/README.md b/README.md index 405453d..b0acfdd 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ s3 = boto3.client('s3') try: s3.get_object(Bucket='my-bucket', Key='example') except errors.NoSuchBucket as error: +# or +# except catch_aws_error('NoSuchBucket') as error: print(error.message) # error handling ``` @@ -115,6 +117,8 @@ except (errors.NoSuchBucket, errors.NoSuchKey) as error: # error handling ``` +You can catch all `ClientError`s with `errors.ALL`. + You can only use this style for error codes that work as Python property names. For error codes like EC2's `InvalidInstanceID.NotFound`, you have to use `catch_aws_error()` (see below). diff --git a/aws_error_utils/aws_error_utils.py b/aws_error_utils/aws_error_utils.py index 2ca1277..276565b 100644 --- a/aws_error_utils/aws_error_utils.py +++ b/aws_error_utils/aws_error_utils.py @@ -20,7 +20,7 @@ operation_name = e.operation_name """ -__version__ = "2.5.0" # update here and pyproject.toml +__version__ = "2.6.0" # update here and pyproject.toml __all__ = [ "AWSErrorInfo", @@ -187,6 +187,8 @@ def __getattr__(self, name) -> Type[BaseException]: self.__name__, name ) ) + if name in ["ALL", "ALL_CODES"]: + name = ALL_CODES return catch_aws_error(name) diff --git a/pyproject.toml b/pyproject.toml index 5e7acfc..481a463 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aws-error-utils" -version = "2.5.0" # update here and aws_error_utils.py +version = "2.6.0" # update here and aws_error_utils.py description = "Error-handling functions for boto3/botocore" authors = ["Ben Kehoe"] license = "Apache-2.0" diff --git a/test_aws_error_utils.py b/test_aws_error_utils.py index 1c892d4..8df8570 100644 --- a/test_aws_error_utils.py +++ b/test_aws_error_utils.py @@ -189,6 +189,22 @@ def test_errors(): with pytest.raises(RuntimeError): errors() + try: + raise error + except errors.ALL as e: + pass + except Exception: + assert False + + class OtherError(Exception): + pass + + try: + raise OtherError("test") + except errors.ALL as e: + assert False + except OtherError: + assert True def test_make_aws_error(): args = {