add factory method to bad status to create correct subclass

pull/8879/head
Alexander Polcyn 8 years ago
parent 6002b8ff63
commit acacd0d646
  1. 43
      src/ruby/lib/grpc/errors.rb
  2. 8
      src/ruby/spec/error_sanity_spec.rb

@ -45,6 +45,8 @@ module GRPC
class BadStatus < StandardError class BadStatus < StandardError
attr_reader :code, :details, :metadata attr_reader :code, :details, :metadata
include GRPC::Core::StatusCodes
# @param code [Numeric] the status code # @param code [Numeric] the status code
# @param details [String] the details of the exception # @param details [String] the details of the exception
# @param metadata [Hash] the error's metadata # @param metadata [Hash] the error's metadata
@ -62,6 +64,47 @@ module GRPC
def to_status def to_status
Struct::Status.new(code, details, @metadata) Struct::Status.new(code, details, @metadata)
end end
def self.new_status_exception(code, details = 'unkown cause', metadata = {})
case code
when OK
Ok.new(details, metadata)
when CANCELLED
Cancelled.new(details, metadata)
when UNKNOWN
Unknown.new(details, metadata)
when INVALID_ARGUMENT
InvalidArgument.new(details, metadata)
when DEADLINE_EXCEEDED
DeadlineExceeded.new(details, metadata)
when NOT_FOUND
NotFound.new(details, metadata)
when ALREADY_EXISTS
AlreadyExists.new(details, metadata)
when PERMISSION_DENIED
PermissionDenied.new(details, metadata)
when UNAUTHENTICATED
Unauthenticated.new(details, metadata)
when RESOURCE_EXHAUSTED
ResourceExhausted.new(details, metadata)
when FAILED_PRECONDITION
FailedPrecondition.new(details, metadata)
when ABORTED
Aborted.new(details, metadata)
when OUT_OF_RANGE
OutOfRange.new(details, metadata)
when UNIMPLEMENTED
Unimplemented.new(details, metadata)
when INTERNAL
Internal.new(details, metadata)
when UNAVAILABLE
Unavailable.new(details, metadata)
when DATA_LOSS
DataLoss.new(details, metadata)
else
fail 'unknown code'
end
end
end end
# GRPC status code corresponding to status OK # GRPC status code corresponding to status OK

@ -48,11 +48,17 @@ describe StatusCodes do
error_object = error_class.new error_object = error_class.new
# check that the code matches the int value of the error's constant # check that the code matches the int value of the error's constant
expect(error_object.code).to eq(StatusCodes.const_get(status_name)) status_code = StatusCodes.const_get(status_name)
expect(error_object.code).to eq(status_code)
# check default parameters # check default parameters
expect(error_object.details).to eq('unknown cause') expect(error_object.details).to eq('unknown cause')
expect(error_object.metadata).to eq({}) expect(error_object.metadata).to eq({})
# check that the BadStatus factory for creates the correct
# exception too
from_factory = GRPC::BadStatus.new_status_exception(status_code)
expect(from_factory.is_a?(error_class)).to be(true)
end end
end end
end end

Loading…
Cancel
Save