From 4e749dba4de93020f860c713492eb8b6872da746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barthelet?= Date: Thu, 10 Apr 2025 00:06:07 +0200 Subject: [PATCH] Add isJSONRPCError method --- src/shared/protocol.ts | 3 ++- src/types.ts | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/shared/protocol.ts b/src/shared/protocol.ts index 19ff97d3..07bfc02c 100644 --- a/src/shared/protocol.ts +++ b/src/shared/protocol.ts @@ -3,6 +3,7 @@ import { CancelledNotificationSchema, ClientCapabilities, ErrorCode, + isJSONRPCError, isJSONRPCRequest, isJSONRPCResponse, isJSONRPCNotification, @@ -274,7 +275,7 @@ export abstract class Protocol< }; this._transport.onmessage = (message) => { - if (isJSONRPCResponse(message)) { + if (isJSONRPCResponse(message) || isJSONRPCError(message)) { this._onresponse(message); } else if (isJSONRPCRequest(message)) { this._onrequest(message); diff --git a/src/types.ts b/src/types.ts index a68c3ff1..db6bf125 100644 --- a/src/types.ts +++ b/src/types.ts @@ -150,6 +150,9 @@ export const JSONRPCErrorSchema = z }) .strict(); +export const isJSONRPCError = (value: unknown): value is JSONRPCError => + JSONRPCErrorSchema.safeParse(value).success; + export const JSONRPCMessageSchema = z.union([ JSONRPCRequestSchema, JSONRPCNotificationSchema,