Merge pull request #2601 from dotCooCoo/fix/jsonrpc-parse-error-response

craft-jsonrpc: ParseError builds its JSON-RPC error response
This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 10:04:00 +07:00
commit 4ca84f178c
3 files changed
+41

No files matched your search

+2
View File
@@ -26,6 +26,8 @@ object or array parameters are valid for each method.
The caller may inspect the bounded parsed value before `process` to support an existing legacy
protocol. Parse failures and limit failures are returned explicitly so each transport can choose
its error and connection policy; `parse_error_response` builds the JSON-RPC parse error.
`ParseError::error_response` builds a response for any of these failures: `-32700` for invalid
JSON and `-32600` for a message or batch over its limit, each with a null ID.
The envelope behavior is extracted from StencilCraft's JSON-lines MCP/control server. App-specific
method routing, legacy control requests, and MCP lifecycle policy stay with the app.
+22
View File
@@ -51,6 +51,28 @@ impl std::fmt::Display for ParseError {
}
impl std::error::Error for ParseError {}
impl ParseError {
/// The JSON-RPC error for this failure: `-32700` for input that is not JSON, and `-32600` for
/// a message or batch over its limit, which the server did not read as a request.
pub fn rpc_error(self) -> RpcError {
match self {
Self::InvalidJson => RpcError::new(-32700, "Parse error"),
Self::MessageTooLarge | Self::BatchTooLarge => RpcError::new(-32600, self.to_string()),
}
}
/// Build the error response for this failure. The ID is null because no request ID was read.
pub fn error_response(self) -> Value {
error_response(Value::Null, self.rpc_error())
}
}
impl From<ParseError> for RpcError {
fn from(error: ParseError) -> Self {
error.rpc_error()
}
}
/// Error reading a newline-delimited message. After `TooLong`, close or otherwise discard the
/// remaining input line before reading another message.
#[derive(Debug)]
+17
View File
@@ -113,6 +113,23 @@ fn parse_and_batch_limits_apply_before_dispatch() {
assert_eq!(process(&one, limits, |_| Ok(json!({})), |_| {}).unwrap().unwrap().as_array().unwrap().len(), 1);
}
#[test]
fn parse_failures_map_to_error_responses() {
assert_eq!(ParseError::InvalidJson.error_response(), parse_error_response());
for error in [ParseError::MessageTooLarge, ParseError::BatchTooLarge] {
let reply = error.error_response();
assert_eq!(reply["jsonrpc"], "2.0");
assert_eq!(reply["id"], Value::Null);
assert_eq!(reply["error"]["code"], -32600);
assert_eq!(reply["error"]["message"], error.to_string());
assert_eq!(RpcError::from(error), error.rpc_error());
}
let limits = Limits { max_batch_items: 0, ..Limits::default() };
let input = json!([{"jsonrpc":"2.0", "id":1, "method":"ping"}]);
let error = process(&input, limits, |_| Ok(json!({})), |_| {}).unwrap_err();
assert_eq!(error.error_response()["error"]["code"], -32600);
}
#[test]
fn invalid_utf8_and_excessive_nesting_are_rejected() {
assert_eq!(parse_json(&[0xff], Limits::default()), Err(ParseError::InvalidJson));