Adding decorator for activity exceptions 70/13070/2
authorGulsum Atici <gulsum.atici@canonical.com>
Wed, 22 Mar 2023 13:08:03 +0000 (16:08 +0300)
committerGulsum Atici <gulsum.atici@canonical.com>
Wed, 22 Mar 2023 13:51:20 +0000 (16:51 +0300)
Change-Id: I637ce8d2d1e85ba1fb9349cf67b303548937e159
Signed-off-by: Gulsum Atici <gulsum.atici@canonical.com>
osm_common/temporal_exceptions.py [new file with mode: 0644]

diff --git a/osm_common/temporal_exceptions.py b/osm_common/temporal_exceptions.py
new file mode 100644 (file)
index 0000000..275bc45
--- /dev/null
@@ -0,0 +1,41 @@
+# Copyright 2023 Canonical Ltd.
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from functools import wraps
+
+from temporalio.exceptions import ApplicationError
+
+
+class RetryableException(ApplicationError):
+    def __init__(self, message):
+        super().__init__(message, non_retryable=False)
+
+
+def wrap_exceptions(error_message="Unhandled exception"):
+    def Inner(func):
+        @wraps(func)
+        async def wrapper(*args, **kwargs):
+            try:
+                return await func(*args, **kwargs)
+            except Exception as err:
+                if isinstance(err, RetryableException):
+                    raise err
+                raise ApplicationError(
+                    f"{error_message}: {str(err)}", non_retryable=True
+                ) from err
+
+        return wrapper
+
+    return Inner