twirp-on-rails 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +16 -0
- data/lib/twirp/rails/handler.rb +7 -1
- data/lib/twirp/rails/rescuable.rb +31 -0
- data/lib/twirp/rails/version.rb +1 -1
- data/lib/twirp/rails.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 308f1b4a611dd3496ffa026dae81410bffedffb0a2330ef009c4090b2ce71c24
|
4
|
+
data.tar.gz: dcfba3e14253882f6b7ba0943a89876789fb0d8e68015e397c6143e5417f7d35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 140d92258a215e8de86f641a39aeb7cf6e07034ee3c15e9ae187ed9a8f7d1d45d360a7240ecdc0599069dbe63558de7f2ba9ce15ecf7d7c538968bd96f784596
|
7
|
+
data.tar.gz: 3dcfdf03ddab84f16a8b8a052439b22e982456a7076c2ac54d2580244c16846b17cf70709a72bfa4a0be42743f139624bf338a97ff454d9fba83455be77eef90
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -111,6 +111,22 @@ TODO: Give more examples of handlers
|
|
111
111
|
|
112
112
|
Use `before_action`, `around_action`, and other callbacks you're used to, as we build on [AbstractController::Callbacks](https://api.rubyonrails.org/classes/AbstractController/Callbacks.html).
|
113
113
|
|
114
|
+
### rescue_from
|
115
|
+
|
116
|
+
Use `rescue_from` just like you would in a controller:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
class HaberdasherServiceHandler < Twirp::Rails::Handler
|
120
|
+
rescue_from "ArgumentError" do |error|
|
121
|
+
Twirp::Error.invalid_argument(error.message)
|
122
|
+
end
|
123
|
+
|
124
|
+
rescue_from "Pundit::NotAuthorizedError", :not_authorized
|
125
|
+
|
126
|
+
...
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
114
130
|
### DRY Service Hooks
|
115
131
|
|
116
132
|
Apply [Service Hooks](https://github.com/twitchtv/twirp-ruby/wiki/Service-Hooks) one time across multiple services.
|
data/lib/twirp/rails/handler.rb
CHANGED
@@ -4,6 +4,8 @@ module Twirp
|
|
4
4
|
module Rails
|
5
5
|
class Handler
|
6
6
|
include Twirp::Rails::Callbacks
|
7
|
+
include ActiveSupport::Rescuable
|
8
|
+
using Twirp::Rails::Rescuable
|
7
9
|
|
8
10
|
attr_reader :request, :env
|
9
11
|
attr_reader :action_name
|
@@ -34,7 +36,11 @@ module Twirp
|
|
34
36
|
ActiveSupport::Notifications.instrument("handler_run_callbacks.twirp_rails", handler: self.class.name, action: action_name, env: @env, request: @request) do
|
35
37
|
run_callbacks(:process_action) do
|
36
38
|
ActiveSupport::Notifications.instrument("handler_run.twirp_rails", handler: self.class.name, action: action_name, env: @env, request: @request) do |payload|
|
37
|
-
payload[:response] =
|
39
|
+
payload[:response] = begin
|
40
|
+
send_action(name)
|
41
|
+
rescue => exception
|
42
|
+
rescue_with_handler_and_return(exception) || raise
|
43
|
+
end
|
38
44
|
end
|
39
45
|
end
|
40
46
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Twirp
|
4
|
+
module Rails
|
5
|
+
module Rescuable
|
6
|
+
refine ::ActiveSupport::Rescuable::ClassMethods do
|
7
|
+
# A slightly altered version of ActiveSupport::Rescuable#rescue_with_handler
|
8
|
+
# that returns the result rather than the handled exception
|
9
|
+
def rescue_with_handler_and_return(exception, object: self, visited_exceptions: [])
|
10
|
+
visited_exceptions << exception
|
11
|
+
|
12
|
+
if (handler = handler_for_rescue(exception, object: object))
|
13
|
+
handler.call exception
|
14
|
+
elsif exception
|
15
|
+
if visited_exceptions.include?(exception.cause)
|
16
|
+
nil
|
17
|
+
else
|
18
|
+
rescue_with_handler(exception.cause, object: object, visited_exceptions: visited_exceptions)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
refine ::ActiveSupport::Rescuable do
|
25
|
+
def rescue_with_handler_and_return(exception)
|
26
|
+
self.class.rescue_with_handler_and_return exception, object: self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/twirp/rails/version.rb
CHANGED
data/lib/twirp/rails.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twirp-on-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Morrison
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
12
|
+
date: 2024-10-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/twirp/rails/engine.rb
|
64
64
|
- lib/twirp/rails/handler.rb
|
65
65
|
- lib/twirp/rails/rack/conditional_post.rb
|
66
|
+
- lib/twirp/rails/rescuable.rb
|
66
67
|
- lib/twirp/rails/version.rb
|
67
68
|
- sig/twirp/rails.rbs
|
68
69
|
- twirp-rails.gemspec
|