Skip to content

Commit aa0ed49

Browse files
committed
feat(unrelated): SWFError: add error_code
* add SWFError.error_code * @translate: fill it when given a `ClientError` instance * add unit test Signed-off-by: Yves Bastide <yves@botify.com>
1 parent 9f8be21 commit aa0ed49

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

simpleflow/swf/mapper/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
class SWFError(Exception):
12-
def __init__(self, message: str = "", raw_error: str = "", *args) -> None:
12+
def __init__(self, message: str = "", raw_error: str = "", error_code: str = "", *args) -> None:
1313
"""
1414
Examples:
1515
@@ -59,6 +59,7 @@ def __init__(self, message: str = "", raw_error: str = "", *args) -> None:
5959

6060
self.kind = values[0].strip()
6161
self.type_ = self.kind.lower().strip().replace(" ", "_") if self.kind else None
62+
self.error_code = error_code
6263

6364
@property
6465
def message(self):
@@ -312,6 +313,8 @@ def translate(exceptions, to):
312313
"""
313314

314315
def throw(err, *args, **kwargs):
316+
if isinstance(getattr(err, "response", None), dict) and issubclass(to, SWFError):
317+
raise to(extract_message(err), error_code=extract_error_code(err)) from None
315318
raise to(extract_message(err))
316319

317320
return catch(exceptions, handle_with=throw)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
from botocore.exceptions import ClientError
5+
6+
from simpleflow.swf.mapper.exceptions import ResponseError, translate
7+
8+
9+
def test_translate():
10+
def raiser():
11+
raise ClientError(
12+
error_response={
13+
"Error": {
14+
"Code": "UnknownError",
15+
"Message": "unknown error",
16+
}
17+
},
18+
operation_name="Foo",
19+
)
20+
21+
error_ = translate(ClientError, to=ResponseError)(raiser)
22+
with pytest.raises(ResponseError) as excinfo:
23+
error_()
24+
assert excinfo.value.error_code == "UnknownError"

0 commit comments

Comments
 (0)