Skip to content

Commit 457a6bb

Browse files
authored
Fix error code propagation from user defined errors (#3197)
1 parent 95b6f85 commit 457a6bb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Diff for: flytekit/exceptions/user.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def value(self):
3131

3232
@property
3333
def error_code(self):
34-
return self._ERROR_CODE
34+
return self.value.error_code if hasattr(self.value, "error_code") else self._ERROR_CODE
3535

3636

3737
class FlyteTypeException(FlyteUserException, TypeError):

Diff for: tests/flytekit/unit/bin/test_python_entrypoint.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,20 @@ def t1(a: typing.List[int]) -> typing.List[typing.List[str]]:
10011001
@mock.patch("flytekit.core.data_persistence.FileAccessProvider.put_data")
10021002
@mock.patch("flytekit.core.utils.write_proto_to_file")
10031003
def test_dispatch_execute_custom_error_code_with_flyte_user_runtime_exception(mock_write_to_file, mock_upload_dir, mock_get_data, mock_load_proto):
1004-
class CustomException(FlyteUserRuntimeException):
1004+
class CustomException(Exception):
10051005
_ERROR_CODE = "CUSTOM_ERROR_CODE"
10061006

1007+
@property
1008+
def error_code(self):
1009+
return self._ERROR_CODE
1010+
1011+
# Mimic real logic - executes and wraps any exception in a FlyteUserRuntimeException
1012+
def fake_dispatch_execute(*args, **kwargs):
1013+
try:
1014+
return python_task.execute(**kwargs)
1015+
except Exception as e:
1016+
raise FlyteUserRuntimeException(e) from e
1017+
10071018
mock_get_data.return_value = True
10081019
mock_upload_dir.return_value = True
10091020

@@ -1014,14 +1025,16 @@ class CustomException(FlyteUserRuntimeException):
10141025
)
10151026
) as ctx:
10161027
python_task = mock.MagicMock()
1017-
python_task.dispatch_execute.side_effect = CustomException("custom error")
1028+
python_task.execute.side_effect = CustomException("custom error")
1029+
python_task.dispatch_execute.side_effect = fake_dispatch_execute
10181030

10191031
empty_literal_map = _literal_models.LiteralMap({}).to_flyte_idl()
10201032
mock_load_proto.return_value = empty_literal_map
10211033

10221034
def verify_output(*args, **kwargs):
1023-
assert isinstance(args[0], ErrorDocument)
1024-
assert args[0].error.code == "CUSTOM_ERROR_CODE"
1035+
error_document = args[0]
1036+
assert isinstance(error_document, ErrorDocument)
1037+
assert error_document.error.code == "CUSTOM_ERROR_CODE"
10251038

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

0 commit comments

Comments
 (0)