-
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 5 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
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, message = 'unknown 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 | ||
nkadovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './spec_helper' | ||
require 'src/proto/grpc/testing/test_services_pb' | ||
|
||
RSpec.describe GrpcRest::BaseInterceptor, type: :class do | ||
let(:rpc_service) { Grpc::Testing::TestService::Service.new } | ||
let(:rpc_desc) { Grpc::Testing::TestService::Service.rpc_descs.values.first} | ||
let(:message) { Grpc::Testing::SimpleRequest.new } | ||
|
||
describe '#fail!' do | ||
let(:error_message) { 'some message' } | ||
let(:error_code) { :invalid_argument } | ||
|
||
it 'fails properly' do | ||
request = Gruf::Controllers::Request.new( | ||
method_key: :UnaryCall, | ||
service: rpc_service, | ||
rpc_desc: rpc_desc, | ||
active_call: nil, | ||
message: message) | ||
interceptor = GrpcRest::BaseInterceptor.new(request, Gruf::Error.new) | ||
|
||
expect{ interceptor.fail!(error_code, error_message) }.to raise_error(GRPC::InvalidArgument) do |error| | ||
expect(error.message).to match(error_message) | ||
expect(error.code).to eq(GRPC::Core::StatusCodes::INVALID_ARGUMENT) | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ class GrpcApp < Rails::Application | |
loader.ignore("#{Rails.root}/spec/test_service_pb.rb") | ||
loader.setup | ||
require "#{Rails.root}/spec/test_service_pb.rb" | ||
require "#{Rails.root}/lib/base_interceptor.rb" | ||
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. Not sure if this was the right way of fixing it, but I had to include this so my tests could properly run on the CI. |
||
|
||
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__)) | ||
|
||
|
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.
I'd change this comment to:
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.
Makes sense, less filler words. 👍