Merge pull request #137 from modelcontextprotocol/justin/improve-sse-errors

Improve SSE error code reporting
This commit is contained in:
Justin Spahr-Summers 2025-01-24 13:46:35 +00:00 • committed by GitHub
commit d60b0fb271
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,16 @@
import { EventSource, type ErrorEvent, type EventSourceInit } from "eventsource";
import { Transport } from "../shared/transport.js";
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
import { EventSource, type EventSourceInit } from "eventsource";
export class SseError extends Error {
constructor(
public readonly code: number | undefined,
message: string | undefined,
public readonly event: ErrorEvent,
) {
super(`SSE error: ${message}`);
}
}
/**
* Client transport for SSE: this will connect to a server using Server-Sent Events for receiving
@ -42,7 +52,7 @@ export class SSEClientTransport implements Transport {
this._abortController = new AbortController();
this._eventSource.onerror = (event) => {
const error = new Error(`SSE error: ${JSON.stringify(event)}`);
const error = new SseError(event.code, event.message, event);
reject(error);
this.onerror?.(error);
};