protocol-jsonrpc 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0488651e9e3c97f90a8bfe83c904ade78a8e0532e524fc6b1e97fd7ec2334f0f'
4
- data.tar.gz: 2d9c402bcd8786051868b545afdb858ca862fcd4e699e6910a720467b4d0ab3d
3
+ metadata.gz: b54b4f7232fe6c0daa9a4e589f4db5edd2aba983cbcfc7a4ab880cfaaa2c2ffa
4
+ data.tar.gz: f8df39a2b673213f62e9681e1500d7f7b487aa4c77d925253e3dc019c6c0f8d1
5
5
  SHA512:
6
- metadata.gz: 32a5eaf0c2424ce7a93250a7d164fb98a856eb918422481de1d7348906edbd9817cd87d32f55c1eeef3338c9ff7c531329cdd98bad5512254e4c121ae5442b0f
7
- data.tar.gz: be8901fb47e177223a99175999b3e25798c0709505f558442020cca36d24da59e970114cefdbb907874005fac1c241360a78134e7bba2ca49869fbde3182cab2
6
+ metadata.gz: ee456605301d8af0745a57b453583ba115f711f63a93b24463ad86fafaa32e862832d81359654eb48ded753ba6aa1568e251855ec115858984bed24b6dc4277a
7
+ data.tar.gz: 6683a84ec2f1bdc098dfb09caf187cf34763896067f10711b232f54971b8566a3ffedf13224e3a2b9fbb726d799d4e249b9b761f03b92e938d168402d7fecc5f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2025-06-01
4
+
5
+ - If a reply block for a Notification raises, suppress the error as defined in the spec
6
+
3
7
  ## [0.2.0] - 2025-06-01
4
8
 
5
9
  **Breaking changes**: As I work towarsd a 1.0.0 release, I've changed the interface to ensure a uniform interface for [JSON-RPC 2.0 batch processing](https://www.jsonrpc.org/specification#batch). I believe we have a much more robust implementation now, so I will try to stay more consistent, but please be cautious upgrading until 1.0.0 is released and we finalize the interface.
data/README.md CHANGED
@@ -52,10 +52,8 @@ This wraps a framer and provides higher-level methods for communication.
52
52
  Here's a basic example showing how to create a JSON-RPC connection over a socket pair:
53
53
 
54
54
  ```ruby
55
- require 'protocol/jsonrpc'
56
- require 'protocol/jsonrpc/connection'
57
- require 'protocol/jsonrpc/framer'
58
- require 'socket'
55
+ require "protocol/jsonrpc"
56
+ require "socket"
59
57
 
60
58
  # Create a socket pair for testing
61
59
  client_socket, server_socket = UNIXSocket.pair
@@ -179,6 +177,13 @@ Typically created by replying to a request:
179
177
  # From a request object
180
178
  response = request.reply(19)
181
179
 
180
+ response = request.reply do |message|
181
+ message.params.sum if message.request? && message.method == "add"
182
+ if message.method == "fail"
183
+ raise "Oops, we failed" # gets turned into a error response
184
+ end
185
+ end
186
+
182
187
  # Or directly
183
188
  response = Protocol::Jsonrpc::Response.new(
184
189
  result: 19,
@@ -194,6 +199,11 @@ For error responses:
194
199
  # Create from an error object
195
200
  error = Protocol::Jsonrpc::InvalidParamsError.new("Invalid parameters")
196
201
  error_response = request.reply(error)
202
+
203
+ # Create from a reply that raises
204
+ response = request.reply do |message|
205
+ raise "Oops" # Returns an InternalError response
206
+ end
197
207
  ```
198
208
 
199
209
  Error types represent the standard JSON-RPC error codes:
@@ -206,6 +216,14 @@ Protocol::Jsonrpc::InvalidParamsError
206
216
  Protocol::Jsonrpc::InternalError
207
217
  ```
208
218
 
219
+ Most of the errors happen automatically, but some must be triggered manually.
220
+
221
+ ```ruby
222
+ response = request.reply do |message|
223
+ raise Protocol::Jsonrpc::MethodNotFoundError, "We don't support any methods"
224
+ end
225
+ ```
226
+
209
227
  ## Batch Processing
210
228
 
211
229
  JSON-RPC supports batch requests and responses. The library returns a `Protocol::Jsonrpc::Batch` that acts like an array and provides a `reply` method for processing:
@@ -26,7 +26,9 @@ module Protocol
26
26
  def read(&block)
27
27
  flush
28
28
  frame = read_frame
29
- Message.load(frame.unpack)
29
+ message = Message.load(frame.unpack)
30
+ yield message if block_given?
31
+ message
30
32
  rescue => e
31
33
  InvalidMessage.new(error: e)
32
34
  end
@@ -24,8 +24,10 @@ module Protocol
24
24
  ).freeze
25
25
 
26
26
  # Factory method to create the appropriate error type
27
+ # @param code [Integer] The JSON-RPC error code
28
+ # @param message [String] The error message
29
+ # @param data [#as_json, #to_json] Serializable data to return to the client
27
30
  # @param id [String, Integer] The request ID
28
- # @param error [Hash] The error data from the JSON-RPC response
29
31
  # @return [Error] The appropriate error instance
30
32
  def self.from_message(code:, message:, data: nil, id: nil)
31
33
  case code
@@ -54,17 +56,22 @@ module Protocol
54
56
  error = error.transform_keys(&:to_sym)
55
57
  from_message(id: id, data: data, **error)
56
58
  in Jsonrpc::Error
59
+ error.data ||= data if data
60
+ error.id ||= id if id
57
61
  error
58
62
  in JSON::ParserError
59
63
  ParseError.new("Parse error: #{error.message}", data:, id:)
60
64
  in StandardError
61
65
  InternalError.new(error.message, data:, id:)
62
- else
66
+ in Exception
63
67
  raise error
68
+ else
69
+ raise ArgumentError, "Unknown error type: #{error.class}"
64
70
  end
65
71
  end
66
72
 
67
- attr_reader :data, :code, :id
73
+ attr_accessor :data, :id
74
+ attr_reader :code
68
75
 
69
76
  def initialize(message = nil, data: nil, id: nil)
70
77
  message = nil if message&.empty?
@@ -36,16 +36,17 @@ module Protocol
36
36
  # @return [Message] The parsed message
37
37
  def from_hash(parsed)
38
38
  return InvalidMessage.new(data: parsed.inspect) unless parsed.is_a?(Hash)
39
+
39
40
  jsonrpc = parsed[:jsonrpc]
40
41
 
41
42
  case parsed
42
- in { id:, error: }
43
+ in {id:, error:}
43
44
  ErrorResponse.new(id:, error: Error.from_message(**error), jsonrpc:)
44
- in { id:, result: }
45
+ in {id:, result:}
45
46
  Response.new(id:, result:, jsonrpc:)
46
- in { id:, method: }
47
+ in {id:, method:}
47
48
  Request.new(id:, method:, params: parsed[:params], jsonrpc:)
48
- in { method: }
49
+ in {method:}
49
50
  Notification.new(method:, params: parsed[:params], jsonrpc:)
50
51
  else
51
52
  InvalidMessage.new(data: parsed.inspect)
@@ -29,10 +29,12 @@ module Protocol
29
29
  def id = nil
30
30
 
31
31
  # Compatibility with the Request
32
- # Yields the notificatino for processing but ignores the result
32
+ # Yields the notification for processing but ignores the result
33
33
  def reply(*, &)
34
34
  yield self if block_given?
35
- nil # notification always returns nil
35
+ nil
36
+ rescue
37
+ nil # JSON-RPC 2.0 spec says notifications should never return, even on error.
36
38
  end
37
39
 
38
40
  def notification? = true
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module Jsonrpc
8
- VERSION = "0.2.0"
8
+ VERSION = "0.2.1"
9
9
  end
10
10
  end
@@ -6,18 +6,18 @@
6
6
  module Protocol
7
7
  module Jsonrpc
8
8
  JSONRPC_VERSION = "2.0"
9
-
10
- autoload :Batch, "protocol/jsonrpc/batch"
11
- autoload :Connection, "protocol/jsonrpc/connection"
12
- autoload :Error, "protocol/jsonrpc/error"
13
- autoload :ErrorResponse, "protocol/jsonrpc/error_response"
14
- autoload :Frame, "protocol/jsonrpc/frame"
15
- autoload :Framer, "protocol/jsonrpc/framer"
16
- autoload :InvalidMessage, "protocol/jsonrpc/invalid_message"
17
- autoload :Message, "protocol/jsonrpc/message"
18
- autoload :Notification, "protocol/jsonrpc/notification"
19
- autoload :Request, "protocol/jsonrpc/request"
20
- autoload :Response, "protocol/jsonrpc/response"
21
- autoload :VERSION, "protocol/jsonrpc/version"
22
9
  end
23
10
  end
11
+
12
+ require_relative "jsonrpc/version"
13
+ require_relative "jsonrpc/error"
14
+ require_relative "jsonrpc/message"
15
+ require_relative "jsonrpc/error_response"
16
+ require_relative "jsonrpc/invalid_message"
17
+ require_relative "jsonrpc/notification"
18
+ require_relative "jsonrpc/request"
19
+ require_relative "jsonrpc/response"
20
+ require_relative "jsonrpc/frame"
21
+ require_relative "jsonrpc/framer"
22
+ require_relative "jsonrpc/connection"
23
+ require_relative "jsonrpc/batch"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-jsonrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Emde