fix: guard StreamAPIException against empty / non-integer error bodies#217
Merged
Merged
Conversation
StreamAPIException#error_code and #error_message are typed as non-nilable Integer/String, but the constructor only assigned them on the happy path. When the HTTP body was empty or non-JSON (e.g. a load balancer emitting a bare 503), the JSON::ParserError rescue left both unset, so any caller that read error_code hit a Sorbet TypeError (Expected Integer, got NilClass) that masked the underlying HTTP failure. A non-integer "code" (seen on edge rate-limit responses) raised the same TypeError from inside initialize. Seed both readers with sentinels (-1 / 'unknown') before parsing and only overwrite them when the parsed body is a Hash carrying values of the right type. error_code is now always an Integer, message() still falls back to the HTTP status, and a missing or non-integer code no longer raises. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
JimmyPettersson85
approved these changes
Jun 30, 2026
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.
Ticket
Problem
StreamAPIException#error_code(Integer) and#error_message(String) are typed as non-nilable, but the constructor only assigned them on the JSON happy path:Two real failure modes hit this:
Empty / non-JSON body (this ticket). When the API is fronted by a load balancer that emits a bare
503with an empty body,JSON.parse("")raisesJSON::ParserError. The rescue sets@json_response = falsebut never assigns@error_code/@error_message, so they staynil. The exception constructs fine, but the moment a caller readse.error_code(e.g. for logging) the Sorbetsig { returns(Integer) }on theattr_readerruntime-checks the value, seesnil, and raisesTypeError: Expected type Integer, got NilClass (errors.rb:9)— masking the real HTTP error.Reported by Homebase on
stream-chat-ruby 3.9.0:Non-integer
code. An edge rate-limit envelope returningcode: "rate_limit_exceeded"madeT.let(..., Integer)raise aTypeErrorinsideinitialize, so callers couldn't even rescue it as aStreamAPIException(previously reported, ticket #80594).Fix
Seed both readers with sentinels (
-1/'unknown') before parsing, and only overwrite them when the parsed body is aHashcarrying values of the expected type:error_codeis always anInteger— nevernil, never a crash on read.message()still falls back to the HTTP status when there is no usable JSON body.Hashbody no longer raises.This fixes both failure modes at the source rather than relying on the backend to keep emitting a shape the SDK happens to tolerate.
Tests
New network-free
spec/errors_spec.rbcovering: well-formed JSON, empty 503 body, non-JSON body, non-integercode, and a JSON body missingcode/message.Note for reviewers
lib/stream-chat/errors.rbis hand-written (no codegen marker). If the intent is for SDK error types to be self-generated, the same guard should be applied in the generator template instead — flagging for that discussion.Checklist
fix:commit)