Return {} for a response with no body instead of raising TypeError - #64
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
Both Result classes parse the response body with a rescue for
JSON::ParserError only. A response with a nil body -- 204 No Content from
a subscriber destroy, or any provider that answers a success code with an
empty payload -- reaches JSON.parse(nil), which raises TypeError, so
Result.new blows up on a request that actually succeeded.
parse_body / parsed now return {} for a nil or empty body before calling
JSON.parse. successful? still falls through to the status code, so a 204
is reported as a success.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
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.
The bug
Courrier::Email::ResultandCourrier::Subscriber::Resultboth parse the response body like this:The rescue only covers
JSON::ParserError. When the response is present but its body isnil—204 No Contentfrom a subscriberdestroy, or any provider that answers a success status with an empty payload —JSON.parse(nil)raisesTypeError: no implicit conversion of nil into String, soResult.newraises on a request that actually succeeded.The fix
parse_body/parsedreturn{}for anilor empty body before reachingJSON.parse.successful?is unchanged, so it falls through to the status code and a204is still reported as a success.Tests
Added
test_initialize_with_no_content_response(204,nilbody) andtest_initialize_with_empty_bodyto bothresult_test.rbfiles. Thenil-body cases error onmainand pass with the fix.bundle exec rakeandbundle exec standardrbare green (163 runs, 0 failures).🤖 Generated with Claude Code