Skip to content

Commit

Permalink
feat: rewrap certain GRPC errors that swallow the underlying auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-rostovtsev committed Sep 3, 2022
1 parent 457e929 commit 2e05efa
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
5 changes: 4 additions & 1 deletion gapic-common/lib/gapic/call_options/retry_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ def hash
private

def retry? error
error.is_a?(::GRPC::BadStatus) && retry_codes.include?(error.code)
(error.is_a?(::GRPC::BadStatus) ||
error.is_a?(::Gapic::GRPC::AuthorizationError) ||
error.is_a?(::Gapic::GRPC::DeadlineExceededError)) &&
retry_codes.include?(error.code)
end

def delay!
Expand Down
1 change: 1 addition & 0 deletions gapic-common/lib/gapic/grpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
# limitations under the License.

require "grpc"
require "gapic/grpc/errors"
require "gapic/grpc/service_stub"
require "gapic/grpc/status_details"
60 changes: 60 additions & 0 deletions gapic-common/lib/gapic/grpc/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "googleauth"
require "gapic/common/error"

module Gapic
module GRPC
##
# An error class to represent the Authorization Error.
# The GRPC layer wraps auth plugin errors in ::GRPC::Unavailable.
# This class rewraps those GRPC layer errors, presenting a correct status code.
#
class AuthorizationError < ::GRPC::Unauthenticated
end

##
# An error class that represents Deadline Exceeded error with an optional
# retry root cause.
#
# The GRPC layer throws ::GRPC::DeadlineExceeded without any context.
# If the deadline was exceeded while retrying another exception (e.g.
# ::GRPC::Unavailable), that exception could be useful for understanding
# the readon for the timeout.
#
# This exception rewraps ::GRPC::DeadlineExceeded, adding an exception
# that was being retried until the deadline was exceeded (if any) as a
# `root_cause` attribute.
#
# @!attribute [r] root_cause
# @return [Object, nil] The exception that was being retried
# when the DeadlineExceeded error occured.
#
class DeadlineExceededError < ::GRPC::DeadlineExceeded
attr_reader :root_cause

##
# @param message [String] The error message.
#
# @param root_cause [Object, nil] The exception that was being retried
# when the DeadlineExceeded error occured.
#
def initialize message, root_cause: nil
super message
@root_cause = root_cause
end
end
end
end
13 changes: 12 additions & 1 deletion gapic-common/lib/gapic/grpc/service_stub/rpc_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

require "gapic/call_options"
require "grpc/errors"

module Gapic
class ServiceStub
Expand Down Expand Up @@ -116,13 +117,23 @@ def call request, options: nil
deadline = calculate_deadline options
metadata = options.metadata

retried_exception = nil
begin
operation = stub_method.call request, deadline: deadline, metadata: metadata, return_op: true
response = operation.execute
yield response, operation if block_given?
response
rescue ::GRPC::DeadlineExceeded => e
raise Gapic::GRPC::DeadlineExceededError.new e.message, root_cause: retried_exception
rescue StandardError => e
retry if check_retry?(deadline) && options.retry_policy.call(e)
if e.is_a?(::GRPC::Unavailable) && /Signet::AuthorizationError/ =~ e.message
e = Gapic::GRPC::AuthorizationError.new e.message.gsub(%r{^\d+:}, "")
end

if check_retry?(deadline) && options.retry_policy.call(e)
retried_exception = e
retry
end

raise e
end
Expand Down

0 comments on commit 2e05efa

Please sign in to comment.