Exception: JSON_RPC::JsonRpcError
- Inherits:
-
StandardError
- Object
- StandardError
- JSON_RPC::JsonRpcError
- Defined in:
- lib/json_rpc/json_rpc_error.rb
Overview
Custom exception class for JSON-RPC errors, based on the JSON-RPC 2.0 specification.
Constant Summary collapse
- ERROR_CODES =
Define the standard JSON-RPC 2.0 error codes
{ parse_error: { code: -32_700, message: "Parse error" }, invalid_request: { code: -32_600, message: "Invalid Request" }, method_not_found: { code: -32_601, message: "Method not found" }, invalid_params: { code: -32_602, message: "Invalid params" }, internal_error: { code: -32_603, message: "Internal error" }, # Implementation-defined server-errors -32000 to -32099 server_error: { code: -32_000, message: "Server error" } }.freeze
Instance Attribute Summary collapse
- #code ⇒ Integer, Object readonly
- #data ⇒ Integer, Object readonly
Class Method Summary collapse
-
.[](symbol) ⇒ Hash
Retrieve error details by symbol.
-
.build(symbol, message: nil, data: nil) ⇒ Hash
Build an error hash, allowing custom message or data to override defaults.
-
.find_by_code(code) ⇒ Hash?
Retrieve error details by code.
Instance Method Summary collapse
-
#as_json ⇒ Object
For ActiveSupport::JSON.
-
#initialize(symbol, message: nil, data: nil) ⇒ JsonRpcError
constructor
Initialize the error using a symbol key, with optional custom message and data.
-
#to_h ⇒ Hash
Returns a hash formatted for a JSON-RPC error response object (the value of the ‘error’ key).
Constructor Details
#initialize(symbol, message: nil, data: nil) ⇒ JsonRpcError
Initialize the error using a symbol key, with optional custom message and data.
74 75 76 77 78 79 |
# File 'lib/json_rpc/json_rpc_error.rb', line 74 def initialize(symbol, message: nil, data: nil) error_details = self.class.build(symbol, message: , data: data) @code = error_details[:code] @data = error_details[:data] super(error_details[:message]) end |
Instance Attribute Details
#code ⇒ Integer, Object (readonly)
37 38 39 |
# File 'lib/json_rpc/json_rpc_error.rb', line 37 def code @code end |
#data ⇒ Integer, Object (readonly)
37 38 39 |
# File 'lib/json_rpc/json_rpc_error.rb', line 37 def data @data end |
Class Method Details
.[](symbol) ⇒ Hash
Retrieve error details by symbol.
44 45 46 |
# File 'lib/json_rpc/json_rpc_error.rb', line 44 def self.[](symbol) ERROR_CODES[symbol] or raise ArgumentError, "Unknown error symbol: #{symbol}" end |
.build(symbol, message: nil, data: nil) ⇒ Hash
Build an error hash, allowing custom message or data to override defaults.
62 63 64 65 66 67 |
# File 'lib/json_rpc/json_rpc_error.rb', line 62 def self.build(symbol, message: nil, data: nil) error = self[symbol].dup error[:message] = if error[:data] = data if data error end |
.find_by_code(code) ⇒ Hash?
Retrieve error details by code.
52 53 54 |
# File 'lib/json_rpc/json_rpc_error.rb', line 52 def self.find_by_code(code) ERROR_CODES.values.find { |details| details[:code] == code } end |
Instance Method Details
#as_json ⇒ Object
For ActiveSupport::JSON
91 |
# File 'lib/json_rpc/json_rpc_error.rb', line 91 def as_json(*) = to_h |
#to_h ⇒ Hash
Returns a hash formatted for a JSON-RPC error response object (the value of the ‘error’ key).
84 85 86 87 88 |
# File 'lib/json_rpc/json_rpc_error.rb', line 84 def to_h hash = { code: code, message: } hash[:data] = data if data hash end |