Skip to content

Commit

Permalink
Merge pull request #1330 from PrefectHQ/add-sh-log
Browse files Browse the repository at this point in the history
Add error log for exceptions raised in state handlers with tests
  • Loading branch information
cicdw authored Aug 7, 2019
2 parents faa6999 + 7e25f74 commit 5190650
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ These changes are available in the [master branch](https://github.com/PrefectHQ/
- Allow passing of custom headers in `Client` calls - [#1255](https://github.com/PrefectHQ/prefect/pull/1255)
- Autogenerate informative names and tags for Docker images built for Flow storage - [#1237](https://github.com/PrefectHQ/prefect/issues/1237)
- Allow mixed-case configuration keys (environment variables are interpolated as lowercase) - [#1288](https://github.com/PrefectHQ/prefect/issues/1288)
- Ensure state handler errors are logged informatively - [#1326](https://github.com/PrefectHQ/prefect/issues/1326)

### Task Library

Expand Down
2 changes: 1 addition & 1 deletion src/prefect/engine/cloud/task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def call_runner_target_handlers(self, old_state: State, new_state: State) -> Sta
)
except Exception as exc:
msg = "Exception raised while calling state handlers: {}".format(repr(exc))
self.logger.debug(msg)
self.logger.error(msg)
if raise_on_exception:
raise exc
new_state = Failed(msg, result=exc)
Expand Down
6 changes: 3 additions & 3 deletions src/prefect/engine/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def handle_state_change(self, old_state: State, new_state: State) -> State:
except Exception as exc:
if raise_on_exception:
raise
raise ENDRUN(
Failed("Exception raised while calling state handlers.", result=exc)
)
msg = "Exception raised while calling state handlers: {}".format(repr(exc))
self.logger.error(msg)
raise ENDRUN(Failed(msg, result=exc))
return new_state
8 changes: 7 additions & 1 deletion tests/engine/cloud/test_cloud_task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def add(x, y):
assert states[2].cached_inputs == dict(x=x, y=y)


def test_state_handler_failures_are_handled_appropriately(client):
def test_state_handler_failures_are_handled_appropriately(client, caplog):
def bad(*args, **kwargs):
raise SyntaxError("Syntax Errors are nice because they're so unique")

Expand All @@ -604,6 +604,12 @@ def do_nothing():
assert states[1].is_failed()
assert isinstance(states[1].result, SyntaxError)

error_logs = [r.message for r in caplog.records if r.levelname == "ERROR"]
assert len(error_logs) == 1
assert "SyntaxError" in error_logs[0]
assert "unique" in error_logs[0]
assert "state handler" in error_logs[0]


def test_task_runner_performs_retries_for_short_delays(client):
global_list = []
Expand Down
15 changes: 15 additions & 0 deletions tests/engine/test_task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,21 @@ def test_task_handler_that_doesnt_return_state_or_none(self):
with prefect.utilities.debug.raise_on_exception():
TaskRunner(task=task).run()

def test_task_handler_errors_are_logged(self, caplog):
def handler(*args, **kwargs):
raise SyntaxError("oops")

task = Task(state_handlers=[handler])
state = TaskRunner(task=task).run()

assert state.is_failed()

error_logs = [r.message for r in caplog.records if r.levelname == "ERROR"]
assert len(error_logs) == 1
assert "SyntaxError" in error_logs[0]
assert "oops" in error_logs[0]
assert "state handler" in error_logs[0]


class TestTaskRunnerStateHandlers:
def test_task_runner_handlers_are_called(self):
Expand Down

0 comments on commit 5190650

Please sign in to comment.