-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from collectiveidea/rescue_from
Allow handlers to use rescue_from in a way familiar to ActionController users
- Loading branch information
Showing
6 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Twirp | ||
module Rails | ||
module Rescuable | ||
refine ::ActiveSupport::Rescuable::ClassMethods do | ||
# A slightly altered version of ActiveSupport::Rescuable#rescue_with_handler | ||
# that returns the result rather than the handled exception | ||
def rescue_with_handler_and_return(exception, object: self, visited_exceptions: []) | ||
visited_exceptions << exception | ||
|
||
if (handler = handler_for_rescue(exception, object: object)) | ||
handler.call exception | ||
elsif exception | ||
if visited_exceptions.include?(exception.cause) | ||
nil | ||
else | ||
rescue_with_handler(exception.cause, object: object, visited_exceptions: visited_exceptions) | ||
end | ||
end | ||
end | ||
end | ||
|
||
refine ::ActiveSupport::Rescuable do | ||
def rescue_with_handler_and_return(exception) | ||
self.class.rescue_with_handler_and_return exception, object: self | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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