Exception: SurrealDB::ServerError
- Defined in:
- lib/surrealdb/errors.rb
Overview
Base class for errors returned by the SurrealDB server. Supports both legacy (code + message) and v3 structured (kind + details + cause chain) error formats.
Direct Known Subclasses
AlreadyExistsError, ConfigurationError, InternalServerError, NotAllowedError, NotFoundError, QueryError, SerializationError, ThrownError, ValidationError
Constant Summary collapse
- KIND_TO_CLASS =
Populated after subclasses are defined at the bottom of this file.
{}
Instance Attribute Summary collapse
-
#code ⇒ Integer?
readonly
JSON-RPC error code.
-
#details ⇒ Hash?
readonly
Additional error details.
-
#kind ⇒ String?
readonly
Structured error kind (e.g. "NotFound", "NotAllowed").
-
#server_cause ⇒ ServerError?
readonly
Chained cause from the server.
Class Method Summary collapse
-
.from_response(data) ⇒ ServerError
Builds the appropriate ServerError subclass from a parsed error hash.
Instance Method Summary collapse
-
#find_cause(target_kind) ⇒ ServerError?
Walk the cause chain looking for a specific kind.
- #has_kind?(target_kind) ⇒ Boolean
-
#initialize(message = nil, code: nil, kind: nil, details: nil, server_cause: nil) ⇒ ServerError
constructor
A new instance of ServerError.
Constructor Details
#initialize(message = nil, code: nil, kind: nil, details: nil, server_cause: nil) ⇒ ServerError
Returns a new instance of ServerError.
30 31 32 33 34 35 36 |
# File 'lib/surrealdb/errors.rb', line 30 def initialize( = nil, code: nil, kind: nil, details: nil, server_cause: nil) @code = code @kind = kind @details = details @server_cause = server_cause super() end |
Instance Attribute Details
#code ⇒ Integer? (readonly)
Returns JSON-RPC error code.
19 20 21 |
# File 'lib/surrealdb/errors.rb', line 19 def code @code end |
#details ⇒ Hash? (readonly)
Returns additional error details.
25 26 27 |
# File 'lib/surrealdb/errors.rb', line 25 def details @details end |
#kind ⇒ String? (readonly)
Returns structured error kind (e.g. "NotFound", "NotAllowed").
22 23 24 |
# File 'lib/surrealdb/errors.rb', line 22 def kind @kind end |
#server_cause ⇒ ServerError? (readonly)
Returns chained cause from the server.
28 29 30 |
# File 'lib/surrealdb/errors.rb', line 28 def server_cause @server_cause end |
Class Method Details
.from_response(data) ⇒ ServerError
Builds the appropriate ServerError subclass from a parsed error hash.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/surrealdb/errors.rb', line 63 def self.from_response(data) return new(data.to_s) unless data.is_a?(Hash) kind = data['kind'] = data['message'] || data['msg'] code = data['code'] details = data['details'] raw_cause = data['cause'] cause = raw_cause.is_a?(Hash) ? from_response(raw_cause) : nil klass = KIND_TO_CLASS.fetch(kind, self) klass.new(, code: code, kind: kind, details: details, server_cause: cause) end |
Instance Method Details
#find_cause(target_kind) ⇒ ServerError?
Walk the cause chain looking for a specific kind.
41 42 43 44 45 46 47 48 49 |
# File 'lib/surrealdb/errors.rb', line 41 def find_cause(target_kind) current = self while current return current if current.kind == target_kind current = current.server_cause end nil end |
#has_kind?(target_kind) ⇒ Boolean
53 54 55 |
# File 'lib/surrealdb/errors.rb', line 53 def has_kind?(target_kind) # rubocop:disable Naming/PredicatePrefix !find_cause(target_kind).nil? end |