Exception: GRPC::BadStatus

Inherits:
StandardError
  • Object
show all
Includes:
Core::StatusCodes
Defined in:
src/ruby/lib/grpc/errors.rb

Overview

BadStatus is an exception class that indicates that an error occurred at either end of a GRPC connection. When raised, it indicates that a status error should be returned to the other end of a GRPC connection; when caught it means that this end received a status error.

There is also subclass of BadStatus in this module for each GRPC status. E.g., the GRPC::Cancelled class corresponds to status CANCELLED.

See github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/status.h for detailed descriptions of each status code.

Constant Summary

Constants included from Core::StatusCodes

Core::StatusCodes::ABORTED, Core::StatusCodes::ALREADY_EXISTS, Core::StatusCodes::CANCELLED, Core::StatusCodes::DATA_LOSS, Core::StatusCodes::DEADLINE_EXCEEDED, Core::StatusCodes::FAILED_PRECONDITION, Core::StatusCodes::INTERNAL, Core::StatusCodes::INVALID_ARGUMENT, Core::StatusCodes::NOT_FOUND, Core::StatusCodes::OK, Core::StatusCodes::OUT_OF_RANGE, Core::StatusCodes::PERMISSION_DENIED, Core::StatusCodes::RESOURCE_EXHAUSTED, Core::StatusCodes::UNAUTHENTICATED, Core::StatusCodes::UNAVAILABLE, Core::StatusCodes::UNIMPLEMENTED, Core::StatusCodes::UNKNOWN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, details = 'unknown cause', metadata = {}, debug_error_string = nil) ⇒ BadStatus

Returns a new instance of BadStatus.

Parameters:

  • code (Numeric)

    the status code

  • details (String) (defaults to: 'unknown cause')

    the details of the exception

  • metadata (Hash) (defaults to: {})

    the error's metadata


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'src/ruby/lib/grpc/errors.rb', line 40

def initialize(code,
               details = 'unknown cause',
                = {},
               debug_error_string = nil)
  exception_message = "#{code}:#{details}"
  if debug_error_string
    exception_message += ". debug_error_string:#{debug_error_string}"
  end
  super(exception_message)
  @code = code
  @details = details
  @metadata = 
  @debug_error_string = debug_error_string
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code


33
34
35
# File 'src/ruby/lib/grpc/errors.rb', line 33

def code
  @code
end

#debug_error_stringObject (readonly)

Returns the value of attribute debug_error_string


33
34
35
# File 'src/ruby/lib/grpc/errors.rb', line 33

def debug_error_string
  @debug_error_string
end

#detailsObject (readonly)

Returns the value of attribute details


33
34
35
# File 'src/ruby/lib/grpc/errors.rb', line 33

def details
  @details
end

#metadataObject (readonly)

Returns the value of attribute metadata


33
34
35
# File 'src/ruby/lib/grpc/errors.rb', line 33

def 
  @metadata
end

Class Method Details

.new_status_exception(code, details = 'unknown cause', metadata = {}, debug_error_string = nil) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'src/ruby/lib/grpc/errors.rb', line 77

def self.new_status_exception(code,
                              details = 'unknown cause',
                               = {},
                              debug_error_string = nil)
  codes = {}
  codes[OK] = Ok
  codes[CANCELLED] = Cancelled
  codes[UNKNOWN] = Unknown
  codes[INVALID_ARGUMENT] = InvalidArgument
  codes[DEADLINE_EXCEEDED] = DeadlineExceeded
  codes[NOT_FOUND] = NotFound
  codes[ALREADY_EXISTS] = AlreadyExists
  codes[PERMISSION_DENIED] = PermissionDenied
  codes[UNAUTHENTICATED] = Unauthenticated
  codes[RESOURCE_EXHAUSTED] = ResourceExhausted
  codes[FAILED_PRECONDITION] = FailedPrecondition
  codes[ABORTED] = Aborted
  codes[OUT_OF_RANGE] = OutOfRange
  codes[UNIMPLEMENTED] = Unimplemented
  codes[INTERNAL] = Internal
  codes[UNAVAILABLE] = Unavailable
  codes[DATA_LOSS] = DataLoss

  if codes[code].nil?
    BadStatus.new(code, details, , debug_error_string)
  else
    codes[code].new(details, , debug_error_string)
  end
end

Instance Method Details

#to_rpc_statusGoogle::Rpc::Status?

Converts the exception to a deserialized Google::Rpc::Status object. Returns `nil` if the `grpc-status-details-bin` trailer could not be converted to a Google::Rpc::Status due to the server not providing the necessary trailers.

Returns:

  • (Google::Rpc::Status, nil)

69
70
71
72
73
74
75
# File 'src/ruby/lib/grpc/errors.rb', line 69

def to_rpc_status
  GoogleRpcStatusUtils.extract_google_rpc_status(to_status)
rescue Google::Protobuf::ParseError => parse_error
  GRPC.logger.warn('parse error: to_rpc_status failed')
  GRPC.logger.warn(parse_error)
  nil
end

#to_statusStruct::Status

Converts the exception to a Struct::Status for use in the networking wrapper layer.

Returns:

  • (Struct::Status)

    with the same code and details


59
60
61
# File 'src/ruby/lib/grpc/errors.rb', line 59

def to_status
  Struct::Status.new(code, details, , debug_error_string)
end