Exception: JSONRPC::Error

Inherits:
StandardError
  • Object
show all
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.

Examples:

Create an error

error = JSONRPC::Error.new(
  "Invalid Request",
  code: -32600,
  data: { detail: "Additional information about the error" }
)

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, code:, data: nil, request_id: nil) ⇒ Error

Creates a new JSON-RPC 2.0 Error object

Examples:

Create an error with code and message

error = JSONRPC::Error.new("Invalid Request", code: -32600)

Create an error with additional data

error = JSONRPC::Error.new("Invalid params", code: -32602, data: { "field" => "missing" })

Parameters:

  • short description of the error

  • a number indicating the error type

  • (defaults to: nil)

    additional error information

  • (defaults to: nil)

    the request identifier

  • (defaults to: nil)
  • (defaults to: nil)

Raises:

  • if code is not an Integer

  • if message is not a String

API:

  • public



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jsonrpc/error.rb', line 93

def initialize(message, code:, data: nil, request_id: nil)
  super(message)

  validate_code(code)
  validate_message(message)

  @code = code
  @message = message
  @data = data
  @request_id = request_id
end

Instance Attribute Details

#codeInteger

Error code indicating the error type

Examples:

Get error code

error.code # => -32600

Set error code

error.code = -32601

Returns:

API:

  • public



45
46
47
# File 'lib/jsonrpc/error.rb', line 45

def code
  @code
end

#dataHash, ...

Additional information about the error (optional)

Examples:

Get error data

error.data # => { "detail" => "Additional info" }

Set error data

error.data = { "field" => "invalid" }

Returns:

API:

  • public



73
74
75
# File 'lib/jsonrpc/error.rb', line 73

def data
  @data
end

#messageString

Short description of the error

Examples:

Get error message

error.message # => "Invalid Request"

Set error message

error.message = "Method not found"

Returns:

API:

  • public



59
60
61
# File 'lib/jsonrpc/error.rb', line 59

def message
  @message
end

#request_idString, ...

The request identifier (optional for notifications)

Examples:

Get request ID

error.request_id # => "1"

Set request ID

error.request_id = "2"

Returns:

API:

  • public



31
32
33
# File 'lib/jsonrpc/error.rb', line 31

def request_id
  @request_id
end

Instance Method Details

#to_hHash

Converts the error to a JSON-compatible Hash

Examples:

Convert error to hash

error.to_h # => { code: -32600, message: "Invalid Request" }

Returns:

  • the error as a JSON-compatible Hash

API:

  • public



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_jsonString

Converts the error to JSON

Examples:

Convert error to JSON

error.to_json # => '{"code":-32600,"message":"Invalid Request"}'

Parameters:

Returns:

  • the error as a JSON string

API:

  • public



129
130
131
# File 'lib/jsonrpc/error.rb', line 129

def to_json(*)
  MultiJson.dump(to_h, *)
end

#to_responseHash

Converts the error to a complete JSON-RPC response

Examples:

Convert error to response

error.to_response # => { jsonrpc: "2.0", error: { code: -32600, message: "Invalid Request" }, id: nil }

Returns:

  • a complete JSON-RPC response with this error

API:

  • public



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

Parameters:

  • the error code

Raises:

  • if code is not an Integer

API:

  • private



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

Parameters:

  • the error message

Raises:

  • if message is not a String

API:

  • private



172
173
174
# File 'lib/jsonrpc/error.rb', line 172

def validate_message(message)
  raise ArgumentError, 'Error message must be a String' unless message.is_a?(String)
end