Skip to content

Improve SSE error code reporting #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/client/sse.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
};
Expand Down
Loading