Exception: Protocol::Jsonrpc::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Protocol::Jsonrpc::Error
- Defined in:
- lib/protocol/jsonrpc/error.rb
Direct Known Subclasses
InternalError, InvalidParamsError, InvalidRequestError, MethodNotFoundError, ParseError
Constant Summary collapse
- PARSE_ERROR =
-32_700
- INVALID_REQUEST =
-32_600
- METHOD_NOT_FOUND =
-32_601
- INVALID_PARAMS =
-32_602
- INTERNAL_ERROR =
-32_603
- ERROR_MESSAGES =
Hash.new("Error").merge( PARSE_ERROR => "Parse error", INVALID_REQUEST => "Invalid Request", METHOD_NOT_FOUND => "Method not found", INVALID_PARAMS => "Invalid params", INTERNAL_ERROR => "Internal error" ).freeze
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.from_message(code:, message:, data: nil, id: nil) ⇒ Error
Factory method to create the appropriate error type.
- .wrap(error, data: nil, id: nil) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(message = nil, data: nil, id: nil) ⇒ Error
constructor
A new instance of Error.
- #reply(id: @id) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(message = nil, data: nil, id: nil) ⇒ Error
Returns a new instance of Error.
76 77 78 79 80 81 82 |
# File 'lib/protocol/jsonrpc/error.rb', line 76 def initialize( = nil, data: nil, id: nil) = nil if &.empty? ||= ERROR_MESSAGES[code] super() @data = data @id = id end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
74 75 76 |
# File 'lib/protocol/jsonrpc/error.rb', line 74 def code @code end |
#data ⇒ Object
Returns the value of attribute data.
73 74 75 |
# File 'lib/protocol/jsonrpc/error.rb', line 73 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
73 74 75 |
# File 'lib/protocol/jsonrpc/error.rb', line 73 def id @id end |
Class Method Details
.from_message(code:, message:, data: nil, id: nil) ⇒ Error
Factory method to create the appropriate error type
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/protocol/jsonrpc/error.rb', line 32 def self.(code:, message:, data: nil, id: nil) case code when PARSE_ERROR ParseError.new(, data:, id:) when INVALID_REQUEST InvalidRequestError.new(, data:, id:) when METHOD_NOT_FOUND MethodNotFoundError.new(, data:, id:) when INVALID_PARAMS InvalidParamsError.new(, data:, id:) when INTERNAL_ERROR InternalError.new(, data:, id:) else new(, data:, id:) end end |
.wrap(error, data: nil, id: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/protocol/jsonrpc/error.rb', line 49 def self.wrap(error, data: nil, id: nil) case error in nil InvalidRequestError.new(data:, id:) in String InternalError.new(error, data:, id:) in Hash error = error.transform_keys(&:to_sym) (id: id, data: data, **error) in Jsonrpc::Error error.data ||= data if data error.id ||= id if id error in JSON::ParserError ParseError.new("Parse error: #{error.}", data:, id:) in StandardError InternalError.new(error., data:, id:) in Exception raise error else raise ArgumentError, "Unknown error type: #{error.class}" end end |
Instance Method Details
#[](key) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/protocol/jsonrpc/error.rb', line 94 def [](key) case key when :code code when :message when :data data else raise KeyError, "Invalid key: #{key}" end end |
#reply(id: @id) ⇒ Object
84 85 86 |
# File 'lib/protocol/jsonrpc/error.rb', line 84 def reply(id: @id) ErrorResponse.new(id:, error: self) end |
#to_h ⇒ Object
88 89 90 91 92 |
# File 'lib/protocol/jsonrpc/error.rb', line 88 def to_h h = {code:, message:} h[:data] = data if data h end |