Skip to content

Commit 3645cb3

Browse files
Check and access metaclass error_code property properly (#3349)
* Check metaclass error_code property properly Signed-off-by: Andres Gomez Ferrer <[email protected]> * Fix Signed-off-by: Andres Gomez Ferrer <[email protected]> * fix lint Signed-off-by: Andres Gomez Ferrer <[email protected]> --------- Signed-off-by: Andres Gomez Ferrer <[email protected]>
1 parent 3aebd23 commit 3645cb3

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

flytekit/exceptions/user.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def value(self):
3131

3232
@property
3333
def error_code(self):
34-
return self.value.error_code if hasattr(self.value, "error_code") else self._ERROR_CODE
34+
if hasattr(self.value, "error_code"):
35+
return self.value.error_code
36+
elif hasattr(type(self.value), "error_code"):
37+
return type(self.value).error_code
38+
else:
39+
return self._ERROR_CODE
3540

3641

3742
class FlyteTypeException(FlyteUserException, TypeError):

tests/flytekit/unit/bin/test_python_entrypoint.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from flytekit.exceptions import user as user_exceptions
3434
from flytekit.exceptions.base import FlyteException
3535
from flytekit.exceptions.scopes import system_entry_point
36-
from flytekit.exceptions.user import FlyteRecoverableException, FlyteUserRuntimeException
36+
from flytekit.exceptions.user import FlyteRecoverableException, FlyteUserRuntimeException, FlyteUserException
3737
from flytekit.models import literals as _literal_models
3838
from flytekit.models.core import errors as error_models, execution
3939
from flytekit.models.core import execution as execution_models
@@ -128,19 +128,24 @@ def verify_output(*args, **kwargs):
128128
_dispatch_execute(ctx, lambda: python_task, "inputs path", "outputs prefix")
129129
assert mock_write_to_file.call_count == 1
130130

131+
class CustomException(FlyteUserException):
132+
_ERROR_CODE = "USER:CustomError"
133+
131134
@pytest.mark.parametrize(
132135
"exception_value",
133136
[
134-
FlyteException("exception", timestamp=1),
135-
FlyteException("exception"),
136-
Exception("exception"),
137+
[FlyteException("exception", timestamp=1), FlyteException.error_code],
138+
[FlyteException("exception"), FlyteException.error_code],
139+
[Exception("exception"), FlyteUserRuntimeException.error_code],
140+
[CustomException("exception"), CustomException.error_code],
137141
]
138142
)
139143
@mock.patch("flytekit.core.utils.load_proto_from_file")
140144
@mock.patch("flytekit.core.data_persistence.FileAccessProvider.get_data")
141145
@mock.patch("flytekit.core.data_persistence.FileAccessProvider.put_data")
142146
@mock.patch("flytekit.core.utils.write_proto_to_file")
143-
def test_dispatch_execute_exception_with_multi_error_files(mock_write_to_file, mock_upload_dir, mock_get_data, mock_load_proto, exception_value: Exception, monkeypatch):
147+
def test_dispatch_execute_exception_with_multi_error_files(mock_write_to_file, mock_upload_dir, mock_get_data, mock_load_proto, exception_value: typing.Tuple[Exception, str], monkeypatch):
148+
exception_value, error_code = exception_value
144149
monkeypatch.setenv("_F_DES", "1")
145150
monkeypatch.setenv("_F_WN", "worker")
146151

@@ -170,7 +175,7 @@ def verify_output(*args, **kwargs):
170175
assert error_filename_base.startswith("error-")
171176
uuid.UUID(hex=error_filename_base[6:], version=4)
172177
assert error_filename_ext == ".pb"
173-
assert container_error.code == "USER:RuntimeError"
178+
assert container_error.code == error_code
174179

175180
mock_write_to_file.side_effect = verify_output
176181
_dispatch_execute(ctx, lambda: python_task, "inputs path", "outputs prefix")

0 commit comments

Comments
 (0)