Exception: Protocol::Jsonrpc::Error

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

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

Class Method Summary collapse

Instance Method Summary collapse

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(message = nil, data: nil, id: nil)
  message = nil if message&.empty?
  message ||= ERROR_MESSAGES[code]
  super(message)
  @data = data
  @id = id
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#idObject

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

Parameters:

  • code (Integer)

    The JSON-RPC error code

  • message (String)

    The error message

  • data (#as_json, #to_json) (defaults to: nil)

    Serializable data to return to the client

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

    The request ID

Returns:

  • (Error)

    The appropriate error instance



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.from_message(code:, message:, data: nil, id: nil)
  case code
  when PARSE_ERROR
    ParseError.new(message, data:, id:)
  when INVALID_REQUEST
    InvalidRequestError.new(message, data:, id:)
  when METHOD_NOT_FOUND
    MethodNotFoundError.new(message, data:, id:)
  when INVALID_PARAMS
    InvalidParamsError.new(message, data:, id:)
  when INTERNAL_ERROR
    InternalError.new(message, data:, id:)
  else
    new(message, 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)
    from_message(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.message}", data:, id:)
  in StandardError
    InternalError.new(error.message, 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
    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_hObject



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