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)


24
25
26
27
28
29
30
31
# File 'lib/error.rb', line 24

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

#causeObject (readonly)

The upstream cause of the error, if available.



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

def cause
  @cause
end

#codeObject (readonly)

The gRPC/Connect Code for this error.



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

def code
  @code
end

#stately_codeObject (readonly)

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



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

def stately_code
  @stately_code
end

Class Method Details

.from(error) ⇒ StatelyDB::Error

Convert any exception into a StatelyDB::Error.

Parameters:

  • error (Exception)

Returns:



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

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)


63
64
65
66
67
68
69
70
71
# File 'lib/error.rb', line 63

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_stringObject



56
57
58
# File 'lib/error.rb', line 56

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