Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 28 additions & 3 deletions packages/sdk-typescript/src/daemon/DaemonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,35 @@ export class DaemonClient {
} catch {
/* body unreadable */
}
const detail =
body && typeof body === 'object' && 'error' in body
? String((body as { error: unknown }).error)
const errorBody =
body && typeof body === 'object' && !Array.isArray(body)
? (body as Record<string, unknown>)
: undefined;
let detail =
errorBody && 'error' in errorBody
? String(errorBody['error'])
: `HTTP ${res.status}`;
// Only unwrap opaque JSON-RPC 5xx responses. Specific top-level errors may
// intentionally hide diagnostic data that should not become display text.
if (
res.status >= 500 &&
errorBody?.['error'] === 'Internal error' &&
typeof errorBody['code'] === 'number'
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
) {
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
const data = errorBody['data'];
if (typeof data === 'string' && data.length > 0) {
detail = data;
} else if (data && typeof data === 'object' && !Array.isArray(data)) {
const record = data as Record<string, unknown>;
const details = record['details'];
const message = record['message'];
if (typeof details === 'string' && details.length > 0) {
detail = details;
} else if (typeof message === 'string' && message.length > 0) {
detail = message;
}
}
}
if (sessionId && res.status === 503 && body && typeof body === 'object') {
const data = body as {
code?: unknown;
Expand Down
105 changes: 105 additions & 0 deletions packages/sdk-typescript/test/unit/DaemonClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4721,6 +4721,111 @@ describe('DaemonClient', () => {
client.setSessionModel('s-1', 'qwen3-coder'),
).rejects.toMatchObject({ status: 404 });
});

it('surfaces JSON-RPC details for generic 5xx failures', async () => {
const body = {
error: 'Internal error',
code: -32603,
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
data: { details: 'Missing credentials' },
};
const { fetch } = recordingFetch(() => jsonResponse(500, body));
const client = new DaemonClient({ baseUrl: 'http://daemon', fetch });

const error = await client
.setSessionModel('s-1', 'qwen3-coder')
.catch((reason: unknown) => reason);

expect(error).toBeInstanceOf(DaemonHttpError);
expect(error).toMatchObject({
status: 500,
body,
message: 'POST /session/:id/model: Missing credentials',
});
});

it.each([
{
name: 'provider message',
status: 500,
body: {
error: 'Internal error',
code: -32603,
data: { message: 'Provider is throttled' },
},
expected: 'Provider is throttled',
},
{
name: 'string data',
status: 500,
body: {
error: 'Internal error',
code: -32603,
data: 'Agent stopped',
},
expected: 'Agent stopped',
},
{
name: 'empty details',
status: 500,
body: {
error: 'Internal error',
code: -32603,
data: { details: '' },
},
expected: 'Internal error',
},
{
name: 'non-string details',
status: 500,
body: {
error: 'Internal error',
code: -32603,
data: { details: { reason: 'private' } },
},
expected: 'Internal error',
},
{
name: 'specific top-level error',
status: 500,
body: {
error: 'Model is unavailable',
code: -32603,
data: { details: 'private provider response' },
},
expected: 'Model is unavailable',
},
{
name: 'non-JSON-RPC code',
status: 500,
body: {
error: 'Internal error',
code: 'internal_error',
data: { details: 'private provider response' },
},
expected: 'Internal error',
},
{
name: 'client error',
status: 400,
body: {
error: 'Internal error',
code: -32603,
data: { details: 'private provider response' },
},
expected: 'Internal error',
},
])('formats $name safely', async ({ status, body, expected }) => {
const { fetch } = recordingFetch(() => jsonResponse(status, body));
const client = new DaemonClient({ baseUrl: 'http://daemon', fetch });
Comment thread
doudouOUC marked this conversation as resolved.
Resolved

await expect(
client.setSessionModel('s-1', 'qwen3-coder'),
).rejects.toMatchObject({
status,
body,
message: `POST /session/:id/model: ${expected}`,
});
});
});

describe('setSessionApprovalMode (#4175 Wave 4 PR 17)', () => {
Expand Down
Loading