-
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.
Allow handlers to use rescue_from in a way familiar to ActionControll…
…er users ActiveSupport::Rescuable does close to what we need, but rather than calling render or redirect, handlers return an object (which may be a Twirp::Error). We refine ActiveSupport::Rescuable to return the result of the rescue_from block/method.
- Loading branch information
1 parent
4ba7392
commit fb085cb
Showing
5 changed files
with
62 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
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