Skip to content

Commit 9fae51f

Browse files
authored
fix(api): include request path in API error messages (#1224)
* fix(api): include request path in API error messages Backport of v1.x #1094 to main. When a Socket API request fails, we now always surface the endpoint path in the error message — not just on the non-ok-response branch where we already had it. Specifically: * `queryApiSafeText` catch block (network error) — append `(path: ${path})` * `queryApiSafeText` response-text-read catch — append `(path: ${path})` * `sendApiRequest` catch block (network error) — append `(path: ${path})` * `sendApiRequest` JSON-parse catch — append `(path: ${path})` The `handleApiCall` / `handleApiCallNoSpinner` paths already include `(endpoint: ${description})` so no change there. Closes the coverage gap flagged in the v1.x changelog: "This lacks the request URL, making it difficult to debug which endpoint failed." * test(api): assert (path: …) suffix on error causes The original #1224 change added `(path: <path>)` to four error causes but the existing tests only asserted loose substrings like "failed" / "parsing" / "response text", so a regression would slip by unnoticed. Adds four targeted assertions inside the existing tests: * queryApiSafeText — network-failure branch * queryApiSafeText — text-read-failure branch * sendApiRequest — network-failure branch * sendApiRequest — JSON-parse-failure branch Each now verifies `result.cause` contains `(path: test/path)`. No new tests added — the existing ones already set up the right mocks for each failure mode, so extending the assertion is the minimum change to actually lock the behavior in.
1 parent 806bc96 commit 9fae51f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/cli/src/utils/socket/api.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ export async function queryApiSafeText(
465465
return {
466466
ok: false,
467467
message,
468-
cause: networkDiagnostics,
468+
cause: `${networkDiagnostics} (path: ${path})`,
469469
}
470470
}
471471

@@ -506,7 +506,7 @@ export async function queryApiSafeText(
506506
return {
507507
ok: false,
508508
message: 'API request failed',
509-
cause: 'Unexpected error reading response text',
509+
cause: `Unexpected error reading response text (path: ${path})`,
510510
}
511511
}
512512
}
@@ -643,7 +643,7 @@ export async function sendApiRequest<T>(
643643
return {
644644
ok: false,
645645
message,
646-
cause: networkDiagnostics,
646+
cause: `${networkDiagnostics} (path: ${path})`,
647647
}
648648
}
649649

@@ -686,7 +686,7 @@ export async function sendApiRequest<T>(
686686
return {
687687
ok: false,
688688
message: 'API request failed',
689-
cause: 'Unexpected error parsing response JSON',
689+
cause: `Unexpected error parsing response JSON (path: ${path})`,
690690
}
691691
}
692692
}

packages/cli/test/unit/utils/socket/api.test.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ describe('api utilities', () => {
469469
expect(result.ok).toBe(false)
470470
if (!result.ok) {
471471
expect(result.message).toContain('failed')
472+
// The cause must include the request path so the user can tell
473+
// which endpoint failed when several calls are in flight.
474+
expect(result.cause).toContain('(path: test/path)')
472475
}
473476
expect(mockFailAndStop).toHaveBeenCalled()
474477
})
@@ -493,6 +496,7 @@ describe('api utilities', () => {
493496
expect(result.ok).toBe(false)
494497
if (!result.ok) {
495498
expect(result.cause).toContain('response text')
499+
expect(result.cause).toContain('(path: test/path)')
496500
}
497501
})
498502
})
@@ -628,6 +632,8 @@ describe('api utilities', () => {
628632
expect(result.ok).toBe(false)
629633
if (!result.ok) {
630634
expect(result.message).toContain('failed')
635+
// Request path must be surfaced in error cause for debuggability.
636+
expect(result.cause).toContain('(path: test/path)')
631637
}
632638
})
633639

@@ -642,6 +648,7 @@ describe('api utilities', () => {
642648
expect(result.ok).toBe(false)
643649
if (!result.ok) {
644650
expect(result.cause).toContain('parsing')
651+
expect(result.cause).toContain('(path: test/path)')
645652
}
646653
})
647654

0 commit comments

Comments
 (0)