Skip to content

fix: malformed terminal message raises AttributeError instead of S2ServerError#30

Merged
quettabit merged 1 commit intomainfrom
qb/iss-26
Apr 11, 2026
Merged

fix: malformed terminal message raises AttributeError instead of S2ServerError#30
quettabit merged 1 commit intomainfrom
qb/iss-26

Conversation

@quettabit
Copy link
Copy Markdown
Member

closes #26

@quettabit quettabit requested a review from a team as a code owner April 11, 2026 06:02
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Apr 11, 2026

Greptile Summary

This PR fixes a bug where a malformed terminal message body — one where json.loads returns a non-dict value (e.g. a JSON array or string) — would cause raise_for_412 or raise_for_416 to be called with that non-dict value, triggering an AttributeError (from .get() in raise_for_416) instead of a proper S2ServerError. The fix moves the status-code-specific raises inside the isinstance(error, dict) guard, consistent with the already-correct pattern in parse_error_info.

Confidence Score: 5/5

Safe to merge — minimal, targeted fix that closes a real bug without side effects.

The change is a one-level restructuring that aligns _handle_terminal with the already-correct parse_error_info pattern. All code paths still raise the appropriate exception type; non-dict error values now correctly fall through to S2ServerError instead of causing an AttributeError. No regressions are apparent.

No files require special attention.

Important Files Changed

Filename Overview
src/s2_sdk/_s2s/_protocol.py Guards raise_for_412/raise_for_416 calls inside isinstance(error, dict) check in _handle_terminal, preventing AttributeError on malformed JSON terminal messages; fix matches the existing pattern in parse_error_info.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Terminal message received] --> B{body length >= 2?}
    B -- No --> C[raise S2ClientError]
    B -- Yes --> D[Extract status_code & error_json]
    D --> E{error_json empty?}
    E -- Yes --> F[raise S2ServerError with status_code]
    E -- No --> G[json.loads error_json]
    G -- JSONDecodeError --> H[raise S2ServerError with raw bytes]
    G -- Success --> I{isinstance error dict?}
    I -- No --> J[message = str error raise S2ServerError]
    I -- Yes --> K{status_code == 412?}
    K -- Yes --> L[raise_for_412 raises specific error]
    K -- No --> M{status_code == 416?}
    M -- Yes --> N[raise_for_416 raises specific error]
    M -- No --> O[message = error.get message raise S2ServerError]
Loading

Reviews (1): Last reviewed commit: "initial commit" | Re-trigger Greptile

@quettabit quettabit merged commit fe08739 into main Apr 11, 2026
5 checks passed
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.

[Detail Bug] S2S SDK crashes on malformed JSON error bodies for 412/416 responses

1 participant