Skip to content

fix: dump recorder output when decorated call raises - #808

Open
deepakganesh78 wants to merge 1 commit into
getsentry:masterfrom
deepakganesh78:fix/issue705-recorder-exception-dump
Open

fix: dump recorder output when decorated call raises#808
deepakganesh78 wants to merge 1 commit into
getsentry:masterfrom
deepakganesh78:fix/issue705-recorder-exception-dump

Conversation

@deepakganesh78

Copy link
Copy Markdown

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Other

Description

The recorder decorator previously dumped captured responses only after the decorated function returned successfully. If the function raised, control left the with self block before dump_to_file() ran, and the context manager reset the registry, losing the recording.

Move the dump into the wrapper's finally path so responses captured before an exception are persisted. If both the decorated function and the dump fail, log the dump failure and preserve the original function exception. A dump failure still propagates normally when the decorated function succeeds.

Related Issues

Fixes #705

PR checklist

  • Read the contributing guidelines
  • Ran the full test suite, mypy, and pre-commit checks locally
  • Added the change to CHANGES

Added/updated tests?

  • Yes

Regression coverage verifies:

  • a request is written when the decorated function subsequently raises;
  • the exact original exception is preserved;
  • a secondary dump failure is logged without masking that exception;
  • dump failures still propagate after successful decorated calls.

Validation

  • python -m pytest . --asyncio-mode=auto --cov-report term-missing --cov responses -q — 232 passed
  • python -m pytest responses/tests/test_recorder.py -q — 10 passed
  • python -m tox -e mypy — both mypy passes succeeded
  • python -m pre_commit run --all-files — all hooks passed
  • responses/_recorder.py — 100% coverage

Always attempt the recorder dump before the context resets its registry. Preserve the decorated function's exception when a secondary dump failure occurs, while logging that failure for diagnostics.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines +186 to +188
@custom_recorder.record(file_path=self.out_file)
def run():
return None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't success include a captured request?

Comment thread responses/_recorder.py
function_raised = False
try:
return function(*args, **kwargs)
except BaseException:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use BaseException? Should dump files be written to if there are syntax errors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_recorder doesn't record when there's an exception

2 participants