fix: preserve error context in CLI failure exceptions#961
Open
itxaiohanglover wants to merge 1 commit into
Open
fix: preserve error context in CLI failure exceptions#961itxaiohanglover wants to merge 1 commit into
itxaiohanglover wants to merge 1 commit into
Conversation
When the Claude CLI subprocess exits non-zero, the SDK was destroying error context through exception flattening: 1. ProcessError used a hardcoded stderr placeholder instead of actual CLI stderr output 2. query.py stringified exceptions via str(e), losing type/attributes 3. receive_messages() re-raised as bare Exception, so consumer except ClaudeSDKError handlers never fired Fix by: - Capturing stderr tail in SubprocessCLITransport and using it in ProcessError instead of the placeholder - Passing the original exception object through the message stream - Re-raising the preserved exception (or ClaudeSDKError as fallback) in receive_messages() so typed exception handlers work correctly Closes anthropics#798
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the Claude CLI subprocess exits non-zero, the SDK was destroying error context through a 3-step exception flattening chain, making failures impossible to diagnose:
subprocess_cli.py—ProcessErrorused hardcoded"Check stderr output for details"instead of actual CLI stderrquery.py— Exceptions stringified viastr(e), losing type and attributes (exit_code,stderr)query.py—receive_messages()re-raised as bareException, so consumerexcept ClaudeSDKErrorhandlers never firedThis PR fixes all three issues:
SubprocessCLITransportnow buffers a bounded tail (8KB) of stderr output and includes it inProcessError.stderrinstead of the placeholder string. Only active when stderr is already piped (callback registered), no behavior change for the non-piped path.receive_messages()now re-raises the preserved exception (or falls back toClaudeSDKError), so consumerexcept ClaudeSDKErrorandexcept ProcessErrorhandlers work correctly.Changes
subprocess_cli.py: Add_stderr_taildeque buffer, capture stderr lines in_handle_stderr(), use captured tail inProcessErrorinstead of hardcoded stringquery.py: Pass exception object in error message dict, re-raise typed exception inreceive_messages()Closes #798
Test plan
ruff checkpassesruff formatpassesmypytype checking passes