Add block support to rescue_from for custom error handling#2
Merged
sebscholl merged 4 commits intozarpay:mainfrom Nov 20, 2025
Merged
Add block support to rescue_from for custom error handling#2sebscholl merged 4 commits intozarpay:mainfrom
sebscholl merged 4 commits intozarpay:mainfrom
Conversation
This feature allows rescue_from to accept a block for custom error
handling, enabling services to:
- Return success despite exceptions
- Provide custom error messages
- Specify custom error types dynamically
The block is executed in a special context that provides success() and
failure() methods, giving full control over the response while maintaining
backward compatibility with existing non-block usage.
Example:
rescue_from ActiveRecord::RecordInvalid do |e|
failure("Failed to save: #{e.message}")
end
rescue_from NetworkError do |e|
# Can recover from error and return success
success({ recovered: true, fallback_used: true })
end
Required for CI build to pass after modifying the gem structure
Appeasing the rubocop.
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
This PR adds block support to the
rescue_frommethod, allowing services to customize error handling with full control over the response.Motivation
Currently,
rescue_fromonly supports a static error type via theuse:parameter. This PR enables dynamic error handling by accepting a block that can:Implementation
The implementation uses a
BlockContextclass that providessuccess()andfailure()methods matching the service interface. The block is executed viainstance_exec, giving a natural API:Key Features
success/failuredirectlyrescue_fromdeclarations continue to workTesting
Comprehensive test coverage includes:
All existing tests pass without modification.
Example Usage