rails_twirp 0.8.0 → 0.11.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: 394816fb648167ae3e089e6e16e93c72f985b5eb540182ff026b8bb283bfa8d8
4
- data.tar.gz: a86a22b695b81fcaf0d93e5df2c33dff327c4fe0c2b54a2cc36c8a5c9a5a4eba
3
+ metadata.gz: 9dc494fd35fa5a920f6ca55c6a024f73bda616a10cc91336fefb3d7520f9c3ed
4
+ data.tar.gz: 5da02487fde28997da7fb200c0a9693fc952fc5662a2e43864d0a12c788ec7d5
5
5
  SHA512:
6
- metadata.gz: 7d0c19ce26c7e5bf06cd9172b9e257dda0ac87f1dec2b6ba6c2b3ecda3aa26664c2319097dc7cf584e3e59316291ec5b7fef59be33ad0564719e2c59aba41ba9
7
- data.tar.gz: 878e8240c50b77f737df1e0d950a4c74d16f06e5fdcd58edccaf00c8471f4f11d645c3e4f8eb5322a500426b4c259ebb028fb389d96608accc74e570e3843866
6
+ metadata.gz: e67eba63003648f4ba6cbc604e4e2006783c5f2873e95745f56bb1f9cfea0660d1f55850b4128b088728f829ef41989c634839b6168d5a512480e465631a60e0
7
+ data.tar.gz: 163e1cd73f34ed7ee62a531c71b6a5153c35231b731645ceba25e22f37e45d94f0a51fc1ff220928eb33d2297964f108cb71696779ea958cd80dc0e4410fe462
@@ -0,0 +1,12 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
7
+
8
+ - package-ecosystem: "github-actions"
9
+ # Checks for workflow files stored in the default location of `.github/workflows`
10
+ directory: "/"
11
+ schedule:
12
+ interval: "weekly"
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.3
data/Gemfile CHANGED
@@ -4,6 +4,15 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "sqlite3"
7
- gem "pbbuilder", "~> 0.10.0"
7
+ gem "pbbuilder", "~> 0.12.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
@@ -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.8.0"
2
+ VERSION = "0.11.0"
3
3
  end
data/rails_twirp.gemspec CHANGED
@@ -13,5 +13,6 @@ Gem::Specification.new do |spec|
13
13
  spec.test_files = `git ls-files -- test/*`.split("\n")
14
14
 
15
15
  spec.add_dependency "rails", ">= 6.1.3"
16
- spec.add_dependency "twirp", "~> 1.7.2"
16
+ spec.add_dependency "twirp", ">= 1.7.2", "~> 1.9.0"
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.8.0
4
+ version: 0.11.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-18 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -28,16 +28,22 @@ dependencies:
28
28
  name: twirp
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.7.2
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.0
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - "~>"
41
+ - - ">="
39
42
  - !ruby/object:Gem::Version
40
43
  version: 1.7.2
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 1.9.0
41
47
  description:
42
48
  email:
43
49
  - bouke@cheddar.me
@@ -45,8 +51,10 @@ executables: []
45
51
  extensions: []
46
52
  extra_rdoc_files: []
47
53
  files:
54
+ - ".github/dependabot.yml"
48
55
  - ".github/workflows/test.yml"
49
56
  - ".gitignore"
57
+ - ".ruby-version"
50
58
  - ".standard.yml"
51
59
  - Gemfile
52
60
  - MIT-LICENSE
@@ -59,6 +67,7 @@ files:
59
67
  - lib/rails_twirp/base.rb
60
68
  - lib/rails_twirp/engine.rb
61
69
  - lib/rails_twirp/errors.rb
70
+ - lib/rails_twirp/exception_handling.rb
62
71
  - lib/rails_twirp/implicit_render.rb
63
72
  - lib/rails_twirp/instrumentation.rb
64
73
  - lib/rails_twirp/log_subscriber.rb
@@ -79,6 +88,7 @@ files:
79
88
  - test/dummy/app/controllers/application_controller.rb
80
89
  - test/dummy/app/controllers/application_twirp_controller.rb
81
90
  - test/dummy/app/controllers/concerns/.keep
91
+ - test/dummy/app/controllers/dummy_controller.rb
82
92
  - test/dummy/app/controllers/pings_controller.rb
83
93
  - test/dummy/app/controllers/testmod/nested/other_controller.rb
84
94
  - test/dummy/app/helpers/application_helper.rb
@@ -88,6 +98,7 @@ files:
88
98
  - test/dummy/app/mailers/application_mailer.rb
89
99
  - test/dummy/app/models/application_record.rb
90
100
  - test/dummy/app/models/concerns/.keep
101
+ - test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder
91
102
  - test/dummy/app/views/layouts/application.html.erb
92
103
  - test/dummy/app/views/layouts/mailer.html.erb
93
104
  - test/dummy/app/views/layouts/mailer.text.erb
@@ -106,7 +117,6 @@ files:
106
117
  - test/dummy/config/environments/production.rb
107
118
  - test/dummy/config/environments/test.rb
108
119
  - test/dummy/config/initializers/application_controller_renderer.rb
109
- - test/dummy/config/initializers/assets.rb
110
120
  - test/dummy/config/initializers/backtrace_silencers.rb
111
121
  - test/dummy/config/initializers/content_security_policy.rb
112
122
  - test/dummy/config/initializers/cookies_serializer.rb
@@ -131,6 +141,7 @@ files:
131
141
  - test/dummy/public/apple-touch-icon-precomposed.png
132
142
  - test/dummy/public/apple-touch-icon.png
133
143
  - test/dummy/public/favicon.ico
144
+ - test/dummy_test.rb
134
145
  - test/other_controller_test.rb
135
146
  - test/ping_controller_test.rb
136
147
  - test/rails_twirp_test.rb
@@ -147,14 +158,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
147
158
  requirements:
148
159
  - - ">="
149
160
  - !ruby/object:Gem::Version
150
- version: '0'
161
+ version: '3'
151
162
  required_rubygems_version: !ruby/object:Gem::Requirement
152
163
  requirements:
153
164
  - - ">="
154
165
  - !ruby/object:Gem::Version
155
166
  version: '0'
156
167
  requirements: []
157
- rubygems_version: 3.2.3
168
+ rubygems_version: 3.2.32
158
169
  signing_key:
159
170
  specification_version: 4
160
171
  summary: Integrate Twirp into Rails
@@ -168,6 +179,7 @@ test_files:
168
179
  - test/dummy/app/controllers/application_controller.rb
169
180
  - test/dummy/app/controllers/application_twirp_controller.rb
170
181
  - test/dummy/app/controllers/concerns/.keep
182
+ - test/dummy/app/controllers/dummy_controller.rb
171
183
  - test/dummy/app/controllers/pings_controller.rb
172
184
  - test/dummy/app/controllers/testmod/nested/other_controller.rb
173
185
  - test/dummy/app/helpers/application_helper.rb
@@ -177,6 +189,7 @@ test_files:
177
189
  - test/dummy/app/mailers/application_mailer.rb
178
190
  - test/dummy/app/models/application_record.rb
179
191
  - test/dummy/app/models/concerns/.keep
192
+ - test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder
180
193
  - test/dummy/app/views/layouts/application.html.erb
181
194
  - test/dummy/app/views/layouts/mailer.html.erb
182
195
  - test/dummy/app/views/layouts/mailer.text.erb
@@ -195,7 +208,6 @@ test_files:
195
208
  - test/dummy/config/environments/production.rb
196
209
  - test/dummy/config/environments/test.rb
197
210
  - test/dummy/config/initializers/application_controller_renderer.rb
198
- - test/dummy/config/initializers/assets.rb
199
211
  - test/dummy/config/initializers/backtrace_silencers.rb
200
212
  - test/dummy/config/initializers/content_security_policy.rb
201
213
  - test/dummy/config/initializers/cookies_serializer.rb
@@ -220,6 +232,7 @@ test_files:
220
232
  - test/dummy/public/apple-touch-icon-precomposed.png
221
233
  - test/dummy/public/apple-touch-icon.png
222
234
  - test/dummy/public/favicon.ico
235
+ - test/dummy_test.rb
223
236
  - test/other_controller_test.rb
224
237
  - test/ping_controller_test.rb
225
238
  - test/rails_twirp_test.rb
@@ -1,12 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Version of your assets, change this if you want to expire all your assets.
4
- Rails.application.config.assets.version = "1.0"
5
-
6
- # Add additional assets to the asset load path.
7
- # Rails.application.config.assets.paths << Emoji.images_path
8
-
9
- # Precompile additional assets.
10
- # application.js, application.css, and all non-JS/CSS in the app/assets
11
- # folder are already added.
12
- # Rails.application.config.assets.precompile += %w( admin.js admin.css )