Skip to content

Return {} for a response with no body instead of raising TypeError - #64

Open
pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/result-nil-body
Open

pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/result-nil-body

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

The bug

Courrier::Email::Result and Courrier::Subscriber::Result both parse the response body like this:

def parse_body(body)
  return {} if @response.nil?

  begin
    JSON.parse(body)
  rescue JSON::ParserError
    {}
  end
end

The rescue only covers JSON::ParserError. When the response is present but its body is nil204 No Content from a subscriber destroy, or any provider that answers a success status with an empty payload — JSON.parse(nil) raises TypeError: no implicit conversion of nil into String, so Result.new raises on a request that actually succeeded.

response = Data.define(:code, :body).new(code: "204", body: nil)
Courrier::Email::Result.new(response: response)
# => TypeError: no implicit conversion of nil into String
Courrier::Subscriber::Result.new(response: response)
# => TypeError: no implicit conversion of nil into String

The fix

parse_body / parsed return {} for a nil or empty body before reaching JSON.parse. successful? is unchanged, so it falls through to the status code and a 204 is still reported as a success.

Tests

Added test_initialize_with_no_content_response (204, nil body) and test_initialize_with_empty_body to both result_test.rb files. The nil-body cases error on main and pass with the fix. bundle exec rake and bundle exec standardrb are green (163 runs, 0 failures).

🤖 Generated with Claude Code

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
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.

1 participant