-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monkey patch gruf interceptor #17
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
019ad05
monkey patch gruf interceptor
nkadovic dd84f71
add gruf as runtime dependency
nkadovic 7905fde
add tests
nkadovic bd77cf1
remove gruf as runtime dependency
nkadovic aed4559
fix tests not running on CI
nkadovic 43b1ae7
Add to README.md
nkadovic 2acc047
re-name test
nkadovic d1dd4cc
fix arity of fail! method
nkadovic 3249014
address cr comments
nkadovic 22e5b1c
minor formatting change
nkadovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ PATH | |
specs: | ||
grpc-rest (0.1.21) | ||
grpc | ||
gruf | ||
rails (>= 6.0) | ||
|
||
GEM | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require 'google/protobuf/well_known_types' | ||
require 'grpc' | ||
require 'grpc/core/status_codes' | ||
require 'interceptors/base_interceptor' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove this line - you can add to the readme that if you want to make an interceptor you have to require this before you do it. |
||
|
||
module GrpcRest | ||
class << self | ||
|
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'gruf' | ||
|
||
module GrpcRest | ||
# This is a monkey-patch that fixes an issue we were having with using the fail! method in | ||
# interceptors where an active call was not instantiated yet. | ||
# Basically, we overloaded this function: https://github.com/bigcommerce/gruf/blob/main/lib/gruf/errors/helpers.rb#L34 | ||
class BaseInterceptor < ::Gruf::Interceptors::ServerInterceptor | ||
def fail!(error_code, _app_code = 500, message = 'unkown error', metadata = {}) | ||
raise grpc_error(error_code, message.to_s, metadata) | ||
end | ||
|
||
private | ||
|
||
# Ported from https://github.com/flipp-oss/grpc-rest/blob/main/lib/grpc_rest.rb#L142 | ||
def grpc_error(error_code, message, metadata) | ||
case error_code | ||
when :ok | ||
GRPC::Ok.new(message, metadata) | ||
when 499 | ||
GRPC::Cancelled.new(message, metadata) | ||
when :bad_request, :invalid_argument | ||
GRPC::InvalidArgument.new(message, metadata) | ||
when :gateway_timeout | ||
GRPC::DeadlineExceeded.new(message, metadata) | ||
when :not_found | ||
GRPC::NotFound.new(message, metadata) | ||
when :conflict | ||
GRPC::AlreadyExists.new(message, metadata) | ||
when :forbidden | ||
GRPC::PermissionDenied.new(message, metadata) | ||
when :unauthorized | ||
GRPC::Unauthenticated.new(message, metadata) | ||
when :too_many_requests | ||
GRPC::ResourceExhausted.new(message, metadata) | ||
when :not_implemented | ||
GRPC::Unimplemented.new(message, metadata) | ||
when :service_unavailable | ||
GRPC::Unavailable.new(message, metadata) | ||
else | ||
GRPC::Internal.new(message, metadata) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gruf
doesn't need to be a runtime dependency. I forgot that I didn't want to enforce this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, sounds good. Just wondering, what is the rationale for not including it? I assumed that since we were explicitly using
Gruf::Interceptors::ServerInterceptor
in the code we'd need it as a dependency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's only in the
send_gruf_request
method, which only gets used if Gruf is loaded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it might be too domain-specific to include in grpc-rest, but I am not sure. Maybe it would be best to include the error interceptor as an example in
README.md
? What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, I think the example we have is fine. I forgot it had some ActiveRecord etc stuff in there.