Use an enum for error codes
This commit is contained in:
parent
4be31d0f08
commit
cd6820e810
2 changed files with 18 additions and 14 deletions
|
|
@ -1,12 +1,10 @@
|
|||
import {
|
||||
CONNECTION_CLOSED_ERROR,
|
||||
INTERNAL_ERROR,
|
||||
ErrorCode,
|
||||
JSONRPCError,
|
||||
JSONRPCNotification,
|
||||
JSONRPCRequest,
|
||||
JSONRPCResponse,
|
||||
McpError,
|
||||
METHOD_NOT_FOUND,
|
||||
Notification,
|
||||
PingRequestSchema,
|
||||
Progress,
|
||||
|
|
@ -124,7 +122,7 @@ export class Protocol<
|
|||
this._transport = undefined;
|
||||
this.onclose?.();
|
||||
|
||||
const error = new McpError(CONNECTION_CLOSED_ERROR, "Connection closed");
|
||||
const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
|
||||
for (const handler of responseHandlers.values()) {
|
||||
handler(error);
|
||||
}
|
||||
|
|
@ -161,7 +159,7 @@ export class Protocol<
|
|||
jsonrpc: "2.0",
|
||||
id: request.id,
|
||||
error: {
|
||||
code: METHOD_NOT_FOUND,
|
||||
code: ErrorCode.MethodNotFound,
|
||||
message: "Method not found",
|
||||
},
|
||||
})
|
||||
|
|
@ -189,7 +187,7 @@ export class Protocol<
|
|||
error: {
|
||||
code: error["code"]
|
||||
? Math.floor(Number(error["code"]))
|
||||
: INTERNAL_ERROR,
|
||||
: ErrorCode.InternalError,
|
||||
message: error.message ?? "Internal error",
|
||||
},
|
||||
});
|
||||
|
|
|
|||
22
src/types.ts
22
src/types.ts
|
|
@ -82,12 +82,20 @@ export const JSONRPCResponseSchema = z
|
|||
})
|
||||
.strict();
|
||||
|
||||
// Standard JSON-RPC error codes
|
||||
export const PARSE_ERROR = -32700;
|
||||
export const INVALID_REQUEST = -32600;
|
||||
export const METHOD_NOT_FOUND = -32601;
|
||||
export const INVALID_PARAMS = -32602;
|
||||
export const INTERNAL_ERROR = -32603;
|
||||
/**
|
||||
* An incomplete set of error codes that may appear in JSON-RPC responses.
|
||||
*/
|
||||
export enum ErrorCode {
|
||||
// SDK error codes
|
||||
ConnectionClosed = -1,
|
||||
|
||||
// Standard JSON-RPC error codes
|
||||
ParseError = -32700,
|
||||
InvalidRequest = -32600,
|
||||
MethodNotFound = -32601,
|
||||
InvalidParams = -32602,
|
||||
InternalError = -32603,
|
||||
}
|
||||
|
||||
/**
|
||||
* A response to a request that indicates an error occurred.
|
||||
|
|
@ -802,8 +810,6 @@ export const ServerResultSchema = z.union([
|
|||
ListToolsResultSchema,
|
||||
]);
|
||||
|
||||
export const CONNECTION_CLOSED_ERROR = -1;
|
||||
|
||||
export class McpError extends Error {
|
||||
constructor(
|
||||
public readonly code: number,
|
||||
|
|
|
|||
Loading…
Reference in a new issue