Exception: JSONRPC::Error
- Inherits:
-
StandardError
- Object
- StandardError
- JSONRPC::Error
- Defined in:
- lib/jsonrpc/error.rb,
sig/jsonrpc/error.rbs
Overview
A JSON-RPC 2.0 Error object
When a rpc call encounters an error, the Response Object must contain an Error object with specific properties according to the JSON-RPC 2.0 specification.
Direct Known Subclasses
InternalError, InvalidParamsError, InvalidRequestError, MethodNotFoundError, ParseError
Instance Attribute Summary collapse
-
#code ⇒ Integer
Error code indicating the error type.
-
#data ⇒ Hash, ...
Additional information about the error (optional).
-
#message ⇒ String
Short description of the error.
-
#request_id ⇒ String, ...
The request identifier (optional for notifications).
Instance Method Summary collapse
-
#initialize(message, code:, data: nil, request_id: nil) ⇒ Error
constructor
Creates a new JSON-RPC 2.0 Error object.
-
#to_h ⇒ Hash
Converts the error to a JSON-compatible Hash.
-
#to_json ⇒ String
Converts the error to JSON.
-
#to_response ⇒ Hash
Converts the error to a complete JSON-RPC response.
-
#validate_code(code) ⇒ void
private
Validates that the code is a valid Integer.
-
#validate_message(message) ⇒ void
private
Validates that the message is a String.
Constructor Details
#initialize(message, code:, data: nil, request_id: nil) ⇒ Error
Creates a new JSON-RPC 2.0 Error object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/jsonrpc/error.rb', line 93 def initialize(, code:, data: nil, request_id: nil) super() validate_code(code) () @code = code @message = @data = data @request_id = request_id end |
Instance Attribute Details
#code ⇒ Integer
Error code indicating the error type
45 46 47 |
# File 'lib/jsonrpc/error.rb', line 45 def code @code end |
#data ⇒ Hash, ...
Additional information about the error (optional)
73 74 75 |
# File 'lib/jsonrpc/error.rb', line 73 def data @data end |
#message ⇒ String
Short description of the error
59 60 61 |
# File 'lib/jsonrpc/error.rb', line 59 def @message end |
#request_id ⇒ String, ...
The request identifier (optional for notifications)
31 32 33 |
# File 'lib/jsonrpc/error.rb', line 31 def request_id @request_id end |
Instance Method Details
#to_h ⇒ Hash
Converts the error to a JSON-compatible Hash
114 115 116 117 118 |
# File 'lib/jsonrpc/error.rb', line 114 def to_h hash = { code:, message: } hash[:data] = data unless data.nil? hash end |
#to_json ⇒ String
Converts the error to JSON
129 130 131 |
# File 'lib/jsonrpc/error.rb', line 129 def to_json(*) MultiJson.dump(to_h, *) end |
#to_response ⇒ Hash
Converts the error to a complete JSON-RPC response
142 143 144 |
# File 'lib/jsonrpc/error.rb', line 142 def to_response Response.new(id: request_id, error: self).to_h end |
#validate_code(code) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates that the code is a valid Integer
158 159 160 |
# File 'lib/jsonrpc/error.rb', line 158 def validate_code(code) raise ArgumentError, 'Error code must be an Integer' unless code.is_a?(Integer) end |
#validate_message(message) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates that the message is a String
172 173 174 |
# File 'lib/jsonrpc/error.rb', line 172 def () raise ArgumentError, 'Error message must be a String' unless .is_a?(String) end |