Class: JSONRPC::Middleware
- Inherits:
-
Object
- Object
- JSONRPC::Middleware
- Defined in:
- lib/jsonrpc/middleware.rb,
sig/jsonrpc/middleware.rbs
Overview
Rack middleware for handling JSON-RPC 2.0 requests
This middleware intercepts HTTP POST requests at a specified path and processes them as JSON-RPC 2.0 messages. It handles parsing, validation, and error responses according to the JSON-RPC 2.0 specification.
Constant Summary collapse
- DEFAULT_PATH =
Default path for JSON-RPC requests
'/'
Instance Method Summary collapse
-
#call(env) ⇒ Array
Rack application call method.
-
#handle_jsonrpc_request ⇒ Array
private
Handles a JSON-RPC request through the complete processing pipeline.
-
#initialize(app, options = {}) ⇒ Middleware
constructor
Initializes the JSON-RPC middleware.
-
#jsonrpc_request? ⇒ Boolean
private
Determines if the current request should be handled as JSON-RPC.
-
#log_internal_error(error) ⇒ void
private
Logs internal errors with full backtrace using the configured logger.
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Initializes the JSON-RPC middleware
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonrpc/middleware.rb', line 46 def initialize(app, = {}) @app = app @path = .fetch(:path, DEFAULT_PATH) @config = JSONRPC.configuration @logger = .fetch(:logger, @config.logger) @parser = Parser.new @validator = Validator.new(logger: @logger) @log_internal_errors = .fetch(:log_internal_errors, @config.log_internal_errors) @rescue_internal_errors = .fetch(:rescue_internal_errors, @config.rescue_internal_errors) end |
Instance Method Details
#call(env) ⇒ Array
Rack application call method
68 69 70 71 72 73 74 75 76 |
# File 'lib/jsonrpc/middleware.rb', line 68 def call(env) @req = Rack::Request.new(env) if jsonrpc_request? handle_jsonrpc_request else @app.call(env) end end |
#handle_jsonrpc_request ⇒ Array
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.
Handles a JSON-RPC request through the complete processing pipeline
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jsonrpc/middleware.rb', line 98 def handle_jsonrpc_request parsed_request = parse_request return parsed_request if parsed_request.is_a?(Array) # Early return for parse errors if @config.validate_procedure_signatures validation_result = validate_request(parsed_request) return validation_result if validation_result.is_a?(Array) # Early return for validation errors end # Set parsed request in environment and call app store_request_in_env(parsed_request) @app.call(@req.env) rescue StandardError => e log_internal_error(e) if @log_internal_errors data = {} data = { class: e.class.name, message: e., backtrace: e.backtrace } if @config.render_internal_errors error = InternalError.new(request_id: parsed_request.is_a?(Request) ? parsed_request.id : nil, data:) @req.env['jsonrpc.error'] = error raise e unless @rescue_internal_errors json_response(200, error.to_response) end |
#jsonrpc_request? ⇒ Boolean
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.
Determines if the current request should be handled as JSON-RPC
86 87 88 |
# File 'lib/jsonrpc/middleware.rb', line 86 def jsonrpc_request? @req.path == @path && @req.post? end |
#log_internal_error(error) ⇒ 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.
Logs internal errors with full backtrace using the configured logger
393 394 395 396 |
# File 'lib/jsonrpc/middleware.rb', line 393 def log_internal_error(error) @logger.error("Internal error: #{error.}") @logger.error(error.backtrace.join("\n")) end |