-
Notifications
You must be signed in to change notification settings - Fork 2.3k
v2 backwards compat: SdkError status code #2049
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
Changes from 4 commits
676bf18
604f95a
37b8c37
4932aae
d9a904f
bfdca86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@modelcontextprotocol/core": minor | ||
| "@modelcontextprotocol/client": minor | ||
| --- | ||
|
|
||
| Add `SdkHttpError` subclass with typed `.status` / `.statusText` accessors for HTTP transport failures. `StreamableHTTPClientTransport` now throws `SdkHttpError` (which extends `SdkError`) for non-OK HTTP responses; `SSEClientTransport` throws `SdkHttpError` for 401-after-reauth (circuit breaker). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,3 +66,45 @@ export class SdkError extends Error { | |
| this.name = 'SdkError'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Typed shape for HTTP error data carried by {@linkcode SdkHttpError}. | ||
| */ | ||
| export interface SdkHttpErrorData { | ||
| status: number; | ||
| statusText?: string; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| /** | ||
| * An {@linkcode SdkError} subclass for HTTP transport failures. | ||
| * | ||
| * Thrown by the streamable HTTP transport when the server responds with a | ||
| * non-OK status code. Narrows {@linkcode SdkError.data | data} to | ||
| * {@linkcode SdkHttpErrorData} so consumers can inspect the HTTP status | ||
| * without unsafe casting. | ||
| * | ||
| * @example | ||
| * ```ts source="./sdkErrors.examples.ts#SdkHttpError_basicUsage" | ||
| * if (error instanceof SdkHttpError) { | ||
| * console.log(error.status); // number | ||
| * console.log(error.statusText); // string | undefined | ||
| * } | ||
| * ``` | ||
|
claude[bot] marked this conversation as resolved.
|
||
| */ | ||
| export class SdkHttpError extends SdkError { | ||
| declare readonly data: SdkHttpErrorData; | ||
|
|
||
| constructor(code: SdkErrorCode, message: string, data: SdkHttpErrorData) { | ||
| super(code, message, data); | ||
| this.name = 'SdkHttpError'; | ||
| } | ||
|
|
||
| get status(): number { | ||
| return this.data.status; | ||
| } | ||
|
|
||
| get statusText(): string | undefined { | ||
| return this.data.statusText; | ||
| } | ||
|
Comment on lines
+107
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 nit: The new Extended reasoning...WhatThis PR adds two new public getters to get status(): number { return this.data.status; }
get statusText(): string | undefined { return this.data.statusText; }The Step-by-step proof
Why this is worth a nit nowThe previous review round (#3247761715) specifically asked the author to populate Why nothing catches itThe getter is a one-line passthrough ( FixAdd a one-liner alongside any of the existing expect((error as SdkHttpError).status).toBe(401);
expect((error as SdkHttpError).statusText).toBe('Unauthorized');Or add a tiny unit test in const e = new SdkHttpError(SdkErrorCode.ClientHttpNotImplemented, 'msg', { status: 500, statusText: 'Internal Server Error' });
expect(e.status).toBe(500);
expect(e.statusText).toBe('Internal Server Error'); |
||
| } | ||
|
claude[bot] marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.