Exception: JSONRPC::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/jsonrpc/error.rb

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" }
)

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:

  • message (String)

    short description of the error

  • code (Integer)

    a number indicating the error type

  • data (Hash, Array, String, Number, Boolean, nil) (defaults to: nil)

    additional error information

  • request_id (String, Integer, nil) (defaults to: nil)

    the request identifier

Raises:

  • (ArgumentError)

    if code is not an Integer

  • (ArgumentError)

    if message is not a String



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

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:

  • (Integer)


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:

  • (Hash, Array, String, Number, Boolean, nil)


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:

  • (String)


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:

  • (String, Integer, nil)


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:

  • (Hash)

    the error as a JSON-compatible Hash



115
116
117
118
119
# File 'lib/jsonrpc/error.rb', line 115

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"}'

Returns:

  • (String)

    the error as a JSON string



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

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:

  • (Hash)

    a complete JSON-RPC response with this error



143
144
145
# File 'lib/jsonrpc/error.rb', line 143

def to_response
  Response.new(id: request_id, error: self).to_h
end