Exception: StatelyDB::Error
- Inherits:
-
StandardError
- Object
- StandardError
- StatelyDB::Error
- Defined in:
- lib/error.rb
Overview
The Error class contains common StatelyDB error types.
Instance Attribute Summary collapse
-
#cause ⇒ Exception
readonly
The upstream cause of the error, if available.
-
#code ⇒ Integer
readonly
The gRPC/Connect Code for this error.
-
#stately_code ⇒ String
readonly
The more fine-grained Stately error code, which is a human-readable string.
Class Method Summary collapse
-
.from(error) ⇒ StatelyDB::Error
Convert any exception into a StatelyDB::Error.
-
.grpc_code_to_string(code) ⇒ String
Turn a gRPC status code into a human-readable string.
Instance Method Summary collapse
-
#code_string ⇒ String
Turn this error’s gRPC status code into a human-readable string.
-
#initialize(message, code: nil, stately_code: nil, cause: nil) ⇒ Error
constructor
A new instance of Error.
Constructor Details
#initialize(message, code: nil, stately_code: nil, cause: nil) ⇒ Error
Returns a new instance of Error.
27 28 29 30 31 32 33 34 |
# File 'lib/error.rb', line 27 def initialize(, code: nil, stately_code: nil, cause: nil) code_str = self.class.grpc_code_to_string(code) super("(#{code_str}/#{stately_code}): #{}") @code = code @stately_code = stately_code @cause = cause end |
Instance Attribute Details
#cause ⇒ Exception (readonly)
The upstream cause of the error, if available.
21 22 23 |
# File 'lib/error.rb', line 21 def cause @cause end |
#code ⇒ Integer (readonly)
The gRPC/Connect Code for this error.
15 16 17 |
# File 'lib/error.rb', line 15 def code @code end |
#stately_code ⇒ String (readonly)
The more fine-grained Stately error code, which is a human-readable 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.
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., code: error.code, stately_code: error_details.stately_code, cause: upstream_cause) end end end new(error., 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”
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_string ⇒ String
Turn this error’s gRPC status code into a human-readable string. e.g. 3 -> “InvalidArgument”
61 62 63 |
# File 'lib/error.rb', line 61 def code_string self.class.grpc_code_to_string(@code) end |