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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f09ec24234c61ab85dc6ab6e03768c16e4fa7110fe5e5771240ca94633996eb
4
- data.tar.gz: 569db17177ad607e4de0a9f94cf380857c48e408f5e7937ed951bd549d5a0431
3
+ metadata.gz: 308f1b4a611dd3496ffa026dae81410bffedffb0a2330ef009c4090b2ce71c24
4
+ data.tar.gz: dcfba3e14253882f6b7ba0943a89876789fb0d8e68015e397c6143e5417f7d35
5
5
  SHA512:
6
- metadata.gz: 542712540c037f4be6ac5f322f45f72a44f55705eb813f941769317afcb17ce1853089524cdf725c61bad411fc73a8ae8be7428a8b3846df6ae9604581d5f90e
7
- data.tar.gz: be401e0761378d95a08bc4d493582450cf5e979e288f578d63c25dd00a69bcc7033b16f77fb31bf0100c2dc1c5a7007fe1c69a8140410b0b4ee5f2238ecfd499
6
+ metadata.gz: 140d92258a215e8de86f641a39aeb7cf6e07034ee3c15e9ae187ed9a8f7d1d45d360a7240ecdc0599069dbe63558de7f2ba9ce15ecf7d7c538968bd96f784596
7
+ data.tar.gz: 3dcfdf03ddab84f16a8b8a052439b22e982456a7076c2ac54d2580244c16846b17cf70709a72bfa4a0be42743f139624bf338a97ff454d9fba83455be77eef90
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+
4
+ ## [1.2.0] - 2024-10-04
5
+
6
+ - Support rescue_from in handlers.
7
+
3
8
  ## [1.1.0] - 2024-09-12
4
9
 
5
10
  - Respect package namespace when looking for handlers.
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.
@@ -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] = send_action(name)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Twirp
4
4
  module Rails
5
- VERSION = "1.1.0"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  end
data/lib/twirp/rails.rb CHANGED
@@ -15,6 +15,7 @@ require_relative "rails/callbacks"
15
15
  require_relative "rails/configuration"
16
16
  require_relative "rails/dispatcher"
17
17
  require_relative "rails/engine"
18
+ require_relative "rails/rescuable"
18
19
  require_relative "rails/handler"
19
20
 
20
21
  module Twirp
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.1.0
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-09-12 00:00:00.000000000 Z
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