rails_twirp 0.7.2 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '054689e120dd41594071de17cd6e8967886699ec4f75bd08aceae475a2e12e03'
4
- data.tar.gz: 56b1471b8103f249c9c2d53fe78b2aa31a978c67f3a1b658e15923534a4a3b63
3
+ metadata.gz: 2e2d7bf09840bf1a4d5aef432df4a47d50e653fe618781324d535c382b222cd9
4
+ data.tar.gz: 7481248258f806ea042f16bd12e09822be2f3d0abcd80c864ab0328572f428ce
5
5
  SHA512:
6
- metadata.gz: b3630e5152913b1bca04ca94ab0d15bdce29f6853a4cafb226c4e5f678042e8a1bbcd6e931b20432d816556ef5f087512e4ad09327e5bbfa3af72e97bcce9a5f
7
- data.tar.gz: bace8057362c6085ba5edea873087b5b4459f6c0dc48e5abeea589380abcd64386dfedfcd41cc2a841f69721b3d852a8516aaf06522df843d8b09e902879842e
6
+ metadata.gz: ed132bcafa1a4801ad6196bb26c76e11dbee3ec64a672363eaf46583189fdc58a6fcc515d5bc4525c5125f6ce06b11a00ac914dbfcc4252c93abedc89a58f4f1
7
+ data.tar.gz: 2469cfc961274f0cac6938951723e425f182becba4b95349c70c981e93f197551068b5124cf3afe687d1f537a3355787b3621275506650c02f5ac1c1c914530e
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.3
data/Gemfile CHANGED
@@ -7,3 +7,12 @@ gem "sqlite3"
7
7
  gem "pbbuilder", "~> 0.10.0"
8
8
  gem "standard"
9
9
  gem "pry"
10
+
11
+ # HACK(bouk): Overwrite Bundler's platform matcher to ignore universal CPU
12
+ # The protobuf and gRPC 'universal' macOS gems break on M1
13
+ module Bundler::MatchPlatform
14
+ def match_platform(p)
15
+ return false if ::Gem::Platform === platform && platform.cpu == "universal"
16
+ Bundler::MatchPlatform.platforms_match?(platform, p)
17
+ end
18
+ end
data/README.md CHANGED
@@ -21,6 +21,21 @@ Or install it yourself as:
21
21
  $ gem install rails_twirp
22
22
  ```
23
23
 
24
+ ## Installing correct protobuf version for M1 Mac Chips
25
+
26
+ If you run into an issue with protobuf universal-darwin version, please paste this in your Gemfile as recommended by @bouke :
27
+
28
+ ```ruby
29
+ # HACK(bouk): Overwrite Bundler's platform matcher to ignore universal CPU
30
+ # The protobuf and gRPC 'universal' macOS gems break on M1
31
+ module Bundler::MatchPlatform
32
+ def match_platform(p)
33
+ return false if ::Gem::Platform === platform && platform.cpu == "universal"
34
+ Bundler::MatchPlatform.platforms_match?(platform, p)
35
+ end
36
+ end
37
+ ```
38
+
24
39
  ## Contributing
25
40
  Contribution directions go here.
26
41
 
@@ -12,6 +12,7 @@ require "rails_twirp/rescue"
12
12
  require "rails_twirp/url_for"
13
13
  require "rails_twirp/implicit_render"
14
14
  require "rails_twirp/instrumentation"
15
+ require "rails_twirp/exception_handling"
15
16
 
16
17
  module RailsTwirp
17
18
  class Base < AbstractController::Base
@@ -36,8 +37,9 @@ module RailsTwirp
36
37
  include AbstractController::Callbacks
37
38
  include Rescue
38
39
  include Instrumentation
40
+ include ExceptionHandling
39
41
 
40
- attr_internal :request, :env, :response_class
42
+ attr_internal :request, :env, :response_class, :rpc_name
41
43
  def initialize
42
44
  @_request = nil
43
45
  @_env = nil
@@ -49,10 +51,11 @@ module RailsTwirp
49
51
  @_http_request ||= ActionDispatch::Request.new(env[:rack_env])
50
52
  end
51
53
 
52
- def dispatch(action, request, response_class, env = {})
54
+ def dispatch(action, request, response_class, rpc_name, env = {})
53
55
  self.request = request
54
56
  self.env = env
55
57
  self.response_class = response_class
58
+ self.rpc_name = rpc_name
56
59
 
57
60
  http_request.controller_instance = self
58
61
 
@@ -61,8 +64,8 @@ module RailsTwirp
61
64
  response_body
62
65
  end
63
66
 
64
- def self.dispatch(action, request, response_class, env = {})
65
- new.dispatch(action, request, response_class, env)
67
+ def self.dispatch(action, request, response_class, rpc_name, env = {})
68
+ new.dispatch(action, request, response_class, rpc_name, env)
66
69
  end
67
70
 
68
71
  # Used by the template renderer to figure out which template to use
@@ -0,0 +1,26 @@
1
+ require "twirp/error"
2
+
3
+ module RailsTwirp
4
+ module ExceptionHandling
5
+ extend ActiveSupport::Concern
6
+
7
+ include AbstractController::Logger
8
+
9
+ def process_action(*)
10
+ super
11
+ rescue Exception => e
12
+ # We adopt the same error handling logic as Rails' standard middlewares:
13
+ # 1. When we 'show exceptions' we make the exception bubble up—this is useful for testing
14
+ raise e unless http_request.show_exceptions?
15
+
16
+ # 2. When we want to show detailed exceptions we include the exception message in the error
17
+ if http_request.get_header("action_dispatch.show_detailed_exceptions")
18
+ self.response_body = Twirp::Error.internal_with(e)
19
+ return
20
+ end
21
+
22
+ # 3. Otherwise we just return a vague internal error message
23
+ self.response_body = Twirp::Error.internal("Internal error")
24
+ end
25
+ end
26
+ end
@@ -17,7 +17,9 @@ module RailsTwirp
17
17
  info do
18
18
  code = payload.fetch(:code, :internal)
19
19
 
20
- message = +"Completed #{code} in #{event.duration.round}ms (Allocations: #{event.allocations})"
20
+ message = +"Completed #{code}"
21
+ message << ": #{payload[:msg]}" if payload[:msg]
22
+ message << " in #{event.duration.round}ms (Allocations: #{event.allocations})"
21
23
  message << "\n\n" if defined?(Rails.env) && Rails.env.development?
22
24
 
23
25
  message
@@ -29,6 +29,8 @@ module RailsTwirp
29
29
 
30
30
  def initialize(service_class)
31
31
  @service_class = service_class
32
+ @service_class.raise_exceptions = true
33
+
32
34
  @rpcs = {}
33
35
  end
34
36
 
@@ -57,7 +59,7 @@ module RailsTwirp
57
59
  controller_name = mapping.controller.underscore
58
60
  const_name = controller_name.camelize << "Controller"
59
61
  controller_class = const_name.constantize
60
- controller_class.dispatch(action_name, req, response_class, env)
62
+ controller_class.dispatch(action_name, req, response_class, name, env)
61
63
  end
62
64
  end
63
65
 
@@ -100,7 +100,7 @@ module RailsTwirp
100
100
  end
101
101
 
102
102
  def set_controller_from_rack_env(env)
103
- @controller = ActionDispatch::Request.new(env).controller_class
103
+ @controller = ActionDispatch::Request.new(env).controller_instance
104
104
  end
105
105
  end
106
106
  end
@@ -1,3 +1,3 @@
1
1
  module RailsTwirp
2
- VERSION = "0.7.2"
2
+ VERSION = "0.10.0"
3
3
  end
data/rails_twirp.gemspec CHANGED
@@ -14,4 +14,5 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  spec.add_dependency "rails", ">= 6.1.3"
16
16
  spec.add_dependency "twirp", "~> 1.7.2"
17
+ spec.required_ruby_version = ">= 3"
17
18
  end
@@ -0,0 +1,5 @@
1
+ class DummyController < RailsTwirp::Base
2
+ def rpc_name_check
3
+ @rpc_name = rpc_name
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ pb.rpc_name @rpc_name
@@ -9,6 +9,7 @@ Rails.application.twirp.routes.draw do
9
9
  rpc "RaiseError", to: "pings#raise_error"
10
10
  rpc "UncaughtError", to: "pings#uncaught_raise"
11
11
  rpc "BeforeError", to: "pings#before_error"
12
+ rpc "RpcNameCheck", to: "dummy#rpc_name_check"
12
13
  end
13
14
 
14
15
  scope module: :testmod do
@@ -13,6 +13,7 @@ service Dummy {
13
13
  rpc UncaughtError(PingRequest) returns (PingResponse);
14
14
  rpc BeforeError(PingRequest) returns (PingResponse);
15
15
  rpc Nested(PingRequest) returns (PingResponse);
16
+ rpc RpcNameCheck(RpcNameCheckRequest) returns (RpcNameCheckResponse);
16
17
  }
17
18
 
18
19
  message PingRequest {
@@ -22,3 +23,8 @@ message PingRequest {
22
23
  message PingResponse {
23
24
  string double_name = 2;
24
25
  }
26
+
27
+ message RpcNameCheckRequest{}
28
+ message RpcNameCheckResponse{
29
+ string rpc_name = 1;
30
+ }
@@ -11,6 +11,11 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
11
11
  add_message "dummy.api.PingResponse" do
12
12
  optional :double_name, :string, 2
13
13
  end
14
+ add_message "dummy.api.RpcNameCheckRequest" do
15
+ end
16
+ add_message "dummy.api.RpcNameCheckResponse" do
17
+ optional :rpc_name, :string, 1
18
+ end
14
19
  end
15
20
  end
16
21
 
@@ -18,5 +23,7 @@ module RPC
18
23
  module DummyAPI
19
24
  PingRequest = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("dummy.api.PingRequest").msgclass
20
25
  PingResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("dummy.api.PingResponse").msgclass
26
+ RpcNameCheckRequest = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("dummy.api.RpcNameCheckRequest").msgclass
27
+ RpcNameCheckResponse = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("dummy.api.RpcNameCheckResponse").msgclass
21
28
  end
22
29
  end
@@ -1,4 +1,4 @@
1
- # Code generated by protoc-gen-twirp_ruby 1.6.0, DO NOT EDIT.
1
+ # Code generated by protoc-gen-twirp_ruby 1.8.0, DO NOT EDIT.
2
2
  require 'twirp'
3
3
  require_relative 'api_pb.rb'
4
4
 
@@ -15,6 +15,7 @@ module RPC
15
15
  rpc :UncaughtError, PingRequest, PingResponse, :ruby_method => :uncaught_error
16
16
  rpc :BeforeError, PingRequest, PingResponse, :ruby_method => :before_error
17
17
  rpc :Nested, PingRequest, PingResponse, :ruby_method => :nested
18
+ rpc :RpcNameCheck, RpcNameCheckRequest, RpcNameCheckResponse, :ruby_method => :rpc_name_check
18
19
  end
19
20
 
20
21
  class DummyClient < Twirp::Client
@@ -0,0 +1,10 @@
1
+ require "test_helper"
2
+
3
+ class DummyControllerTest < RailsTwirp::IntegrationTest
4
+ test "controller gets rpc name" do
5
+ req = RPC::DummyAPI::RpcNameCheckRequest.new
6
+ rpc RPC::DummyAPI::DummyService, "RpcNameCheck", req
7
+ assert_instance_of RPC::DummyAPI::RpcNameCheckResponse, response
8
+ assert_equal "RpcNameCheck", response.rpc_name
9
+ end
10
+ end
@@ -46,12 +46,38 @@ class PingControllerTest < RailsTwirp::IntegrationTest
46
46
  assert_equal :not_found, response.code
47
47
  end
48
48
 
49
- test "uncaught error" do
49
+ test "uncaught errors should bubble up to the test" do
50
+ req = RPC::DummyAPI::PingRequest.new
51
+ assert_raises StandardError, "Uncaught" do
52
+ rpc RPC::DummyAPI::DummyService, "UncaughtError", req
53
+ end
54
+ end
55
+
56
+ test "uncaught errors should return an internal error with details if show_exceptions is true" do
57
+ Rails.application.env_config["action_dispatch.show_exceptions"] = true
58
+
50
59
  req = RPC::DummyAPI::PingRequest.new
51
60
  rpc RPC::DummyAPI::DummyService, "UncaughtError", req
52
61
  assert_instance_of Twirp::Error, response
62
+ assert_equal :internal, response.code
53
63
  assert_equal "Uncaught", response.msg
64
+ assert_equal "StandardError", response.meta["cause"]
65
+ ensure
66
+ Rails.application.env_config["action_dispatch.show_exceptions"] = false
67
+ end
68
+
69
+ test "uncaught errors should return an internal error without if show_exceptions is true and show_detailed_exceptions is false" do
70
+ Rails.application.env_config["action_dispatch.show_exceptions"] = true
71
+ Rails.application.env_config["action_dispatch.show_detailed_exceptions"] = false
72
+
73
+ req = RPC::DummyAPI::PingRequest.new
74
+ rpc RPC::DummyAPI::DummyService, "UncaughtError", req
75
+ assert_instance_of Twirp::Error, response
54
76
  assert_equal :internal, response.code
77
+ assert_equal "Internal error", response.msg
78
+ ensure
79
+ Rails.application.env_config["action_dispatch.show_detailed_exceptions"] = true
80
+ Rails.application.env_config["action_dispatch.show_exceptions"] = false
55
81
  end
56
82
 
57
83
  test "before error" do
@@ -61,4 +87,10 @@ class PingControllerTest < RailsTwirp::IntegrationTest
61
87
  assert_equal "yOuR ReQuEsT Is mAlFoRmEd", response.msg
62
88
  assert_equal :malformed, response.code
63
89
  end
90
+
91
+ test "controller is set to the controller that handled the request" do
92
+ req = RPC::DummyAPI::PingRequest.new(name: "Bouke")
93
+ rpc RPC::DummyAPI::DummyService, "Ping", req
94
+ assert_instance_of PingsController, controller
95
+ end
64
96
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_twirp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bouke van der Bijl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-01 00:00:00.000000000 Z
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".github/workflows/test.yml"
49
49
  - ".gitignore"
50
+ - ".ruby-version"
50
51
  - ".standard.yml"
51
52
  - Gemfile
52
53
  - MIT-LICENSE
@@ -59,6 +60,7 @@ files:
59
60
  - lib/rails_twirp/base.rb
60
61
  - lib/rails_twirp/engine.rb
61
62
  - lib/rails_twirp/errors.rb
63
+ - lib/rails_twirp/exception_handling.rb
62
64
  - lib/rails_twirp/implicit_render.rb
63
65
  - lib/rails_twirp/instrumentation.rb
64
66
  - lib/rails_twirp/log_subscriber.rb
@@ -79,6 +81,7 @@ files:
79
81
  - test/dummy/app/controllers/application_controller.rb
80
82
  - test/dummy/app/controllers/application_twirp_controller.rb
81
83
  - test/dummy/app/controllers/concerns/.keep
84
+ - test/dummy/app/controllers/dummy_controller.rb
82
85
  - test/dummy/app/controllers/pings_controller.rb
83
86
  - test/dummy/app/controllers/testmod/nested/other_controller.rb
84
87
  - test/dummy/app/helpers/application_helper.rb
@@ -88,6 +91,7 @@ files:
88
91
  - test/dummy/app/mailers/application_mailer.rb
89
92
  - test/dummy/app/models/application_record.rb
90
93
  - test/dummy/app/models/concerns/.keep
94
+ - test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder
91
95
  - test/dummy/app/views/layouts/application.html.erb
92
96
  - test/dummy/app/views/layouts/mailer.html.erb
93
97
  - test/dummy/app/views/layouts/mailer.text.erb
@@ -131,6 +135,7 @@ files:
131
135
  - test/dummy/public/apple-touch-icon-precomposed.png
132
136
  - test/dummy/public/apple-touch-icon.png
133
137
  - test/dummy/public/favicon.ico
138
+ - test/dummy_test.rb
134
139
  - test/other_controller_test.rb
135
140
  - test/ping_controller_test.rb
136
141
  - test/rails_twirp_test.rb
@@ -147,14 +152,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
147
152
  requirements:
148
153
  - - ">="
149
154
  - !ruby/object:Gem::Version
150
- version: '0'
155
+ version: '3'
151
156
  required_rubygems_version: !ruby/object:Gem::Requirement
152
157
  requirements:
153
158
  - - ">="
154
159
  - !ruby/object:Gem::Version
155
160
  version: '0'
156
161
  requirements: []
157
- rubygems_version: 3.2.22
162
+ rubygems_version: 3.2.32
158
163
  signing_key:
159
164
  specification_version: 4
160
165
  summary: Integrate Twirp into Rails
@@ -168,6 +173,7 @@ test_files:
168
173
  - test/dummy/app/controllers/application_controller.rb
169
174
  - test/dummy/app/controllers/application_twirp_controller.rb
170
175
  - test/dummy/app/controllers/concerns/.keep
176
+ - test/dummy/app/controllers/dummy_controller.rb
171
177
  - test/dummy/app/controllers/pings_controller.rb
172
178
  - test/dummy/app/controllers/testmod/nested/other_controller.rb
173
179
  - test/dummy/app/helpers/application_helper.rb
@@ -177,6 +183,7 @@ test_files:
177
183
  - test/dummy/app/mailers/application_mailer.rb
178
184
  - test/dummy/app/models/application_record.rb
179
185
  - test/dummy/app/models/concerns/.keep
186
+ - test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder
180
187
  - test/dummy/app/views/layouts/application.html.erb
181
188
  - test/dummy/app/views/layouts/mailer.html.erb
182
189
  - test/dummy/app/views/layouts/mailer.text.erb
@@ -220,6 +227,7 @@ test_files:
220
227
  - test/dummy/public/apple-touch-icon-precomposed.png
221
228
  - test/dummy/public/apple-touch-icon.png
222
229
  - test/dummy/public/favicon.ico
230
+ - test/dummy_test.rb
223
231
  - test/other_controller_test.rb
224
232
  - test/ping_controller_test.rb
225
233
  - test/rails_twirp_test.rb