Raise a helpful ArgumentError for non-hash context - #226
Open
ouchinao wants to merge 1 commit into
Open
Conversation
Passing a non-hash argument to `.call` previously failed with a cryptic "undefined method `each_pair'" error. Context.build now raises an ArgumentError explaining that keyword arguments were likely intended. nil and any object responding to #each_pair are still accepted. Closes collectiveidea#178 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Fixes #178.
Calling an interactor with a non-hash argument — e.g.
MyInteractor.call(something)instead ofMyInteractor.call(something: "value")— currently fails deep inside OpenStruct with a cryptic error:undefined method `each_pair' for #MyInteractor:0x00007fa2fc407350
This gives no hint about what actually went wrong. With this change,
Context.buildraises anArgumentErrorthat points directly at the likely mistake:ArgumentError: Expected a Hash or an object responding to #each_pair,
got MyInteractor. Did you mean to pass keyword arguments?
(e.g. MyInteractor.call(foo: "bar"))
Behavior
Interactor::Contextis preserved, as beforeniland any object responding to#each_pair(Hash,HashWithIndifferentAccess, etc.) are accepted, as before — no breaking change for valid usageArgumentErrorinstead of a confusingNoMethodErrorTesting
nilcase,#each_pair-responding objects, and the new errorstandardrbpasses