Exception: StatelyDB::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/error.rb

Overview

The Error class contains common StatelyDB error types.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, code: nil, stately_code: nil, cause: nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String)
  • code (Integer) (defaults to: nil)
  • stately_code (String) (defaults to: nil)
  • cause (Exception) (defaults to: nil)


27
28
29
30
31
32
33
34
# File 'lib/error.rb', line 27

def initialize(message, code: nil, stately_code: nil, cause: nil)
  code_str = self.class.grpc_code_to_string(code)

  super("(#{code_str}/#{stately_code}): #{message}")
  @code = code
  @stately_code = stately_code
  @cause = cause
end

Instance Attribute Details

#causeException (readonly)

The upstream cause of the error, if available.

Returns:

  • (Exception)


21
22
23
# File 'lib/error.rb', line 21

def cause
  @cause
end

#codeInteger (readonly)

The gRPC/Connect Code for this error.

Returns:

  • (Integer)


15
16
17
# File 'lib/error.rb', line 15

def code
  @code
end

#stately_codeString (readonly)

The more fine-grained Stately error code, which is a human-readable string.

Returns:

  • (String)


18
19
20
# File 'lib/error.rb', line 18

def stately_code
  @stately_code
end

Class Method Details

.from(error) ⇒ StatelyDB::Error

Convert any exception into a StatelyDB::Error.

Parameters:

  • error (Exception)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/error.rb', line 39

def self.from(error)
  return error if error.is_a?(StatelyDB::Error)

  if error.is_a?(GRPC::BadStatus)
    status = error.to_rpc_status

    unless status.nil? || status.details.empty?
      raw_detail = status.details[0]
      if raw_detail.type_url == "type.googleapis.com/stately.errors.StatelyErrorDetails"
        error_details = Stately::Errors::StatelyErrorDetails.decode(raw_detail.value)
        upstream_cause = error_details.upstream_cause.empty? ? nil : StandardError.new(error_details.upstream_cause) # rubocop:disable Metrics/BlockNesting
        return new(error_details.message, code: error.code, stately_code: error_details.stately_code,
                                          cause: upstream_cause)
      end
    end
  end

  new(error.message, code: GRPC::Core::StatusCodes::UNKNOWN, stately_code: "Unknown", cause: error)
end

.grpc_code_to_string(code) ⇒ String

Turn a gRPC status code into a human-readable string. e.g. 3 -> “InvalidArgument”

Parameters:

  • code (Integer)

Returns:

  • (String)


68
69
70
71
72
73
74
75
76
# File 'lib/error.rb', line 68

def self.grpc_code_to_string(code)
  if code > 0
    GRPC::Core::StatusCodes.constants.find do |c|
      GRPC::Core::StatusCodes.const_get(c) === code
    end.to_s.split("_").collect(&:capitalize).join
  else
    "Unknown"
  end
end

Instance Method Details

#code_stringString

Turn this error’s gRPC status code into a human-readable string. e.g. 3 -> “InvalidArgument”

Returns:

  • (String)


61
62
63
# File 'lib/error.rb', line 61

def code_string
  self.class.grpc_code_to_string(@code)
end