Fix NoMethodError when Error is raised with a string argument#653
Merged
crmne merged 3 commits intocrmne:mainfrom Mar 6, 2026
Merged
Fix NoMethodError when Error is raised with a string argument#653crmne merged 3 commits intocrmne:mainfrom
crmne merged 3 commits intocrmne:mainfrom
Conversation
RubyLLM::Error.new("message") puts the string in the `response`
parameter, then `super(message || response&.body)` calls .body on
a String. This surfaces when errors are raised outside the normal
ErrorMiddleware path (e.g., edge cases with provider intermittent
failures).
Detect String arguments in the first position and treat them as
the message instead.
crmne
approved these changes
Mar 6, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #653 +/- ##
==========================================
+ Coverage 81.50% 81.51% +0.01%
==========================================
Files 116 116
Lines 5476 5479 +3
Branches 1430 1432 +2
==========================================
+ Hits 4463 4466 +3
Misses 1013 1013 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
RubyLLM::Error.new("message")puts the string in theresponseparameter, thensuper(message || response&.body)calls.bodyon a String, raisingNoMethodError. This fix detects a String in the first position and treats it as the message instead, keeping full backward compatibility with the two-argErrorMiddlewarepath.Reproduction
Expected: An
Errorinstance withmessage == "something went wrong"andresponse == nilActual:
This happens because the constructor signature is
initialize(response = nil, message = nil)— the string lands inresponse, andsuper(message || response&.body)calls.bodyon it.Existing call sites that trigger this
The normal
ErrorMiddlewarepath always passes both args (response, message) and is unaffected.Test plan
Error.new("msg")no longer raisesNoMethodErrorError.new("msg").messagereturns the stringError.new("msg").responseisnilError.new(response, "msg")still worksError.newstill worksBadRequestError, etc.) inherit the fixErrorMiddlewarespecs pass