Skip to content

Commit b49cd85

Browse files
committed
fix(cli): retain structured upload errors and retry guidance
1 parent 19ddc8e commit b49cd85

5 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎packages/sim-cli/src/commands/source-permissions.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('Search permission CSV upload', () => {
6969
expect(result.stderr).toContain('cancelled')
7070
} finally {
7171
if (originalAny) Object.defineProperty(AbortSignal, 'any', originalAny)
72+
else Reflect.deleteProperty(AbortSignal, 'any')
7273
}
7374
})
7475

‎packages/sim-cli/src/http/client.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ describe('redirects', () => {
103103
})
104104

105105
describe('non-JSON responses', () => {
106+
it.each([200, 429, 503])(
107+
'retains Retry-After when the HTTP %i response body is lost',
108+
async (status) => {
109+
vi.stubGlobal(
110+
'fetch',
111+
vi.fn().mockResolvedValue(
112+
new Response(
113+
new ReadableStream({
114+
start(controller) {
115+
controller.error(new Error('Connection closed'))
116+
},
117+
}),
118+
{ status, headers: { 'retry-after': '10' } }
119+
)
120+
)
121+
)
122+
await expect(client().request('/api/v2/workflows')).rejects.toMatchObject({
123+
code: 'RESPONSE_READ_FAILED',
124+
status,
125+
retryAfterMs: 10_000,
126+
})
127+
}
128+
)
129+
106130
it('turns a 200 that is not JSON into an explained error, not a SyntaxError', async () => {
107131
vi.stubGlobal(
108132
'fetch',

‎packages/sim-cli/src/http/client.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,16 @@ async function readResponseText(response: Response): Promise<string> {
250250
try {
251251
return await response.text()
252252
} catch (error) {
253-
throw new SimApiError(
253+
const failure = new SimApiError(
254254
`Unable to read the response: ${transportErrorMessage(error)}`,
255255
response.status,
256256
'RESPONSE_READ_FAILED',
257257
undefined,
258258
1,
259259
responseRequestId(response)
260260
)
261+
failure.retryAfterMs = parseRetryAfter(response.headers.get('retry-after'), 300_000)
262+
throw failure
261263
}
262264
}
263265

‎packages/sim-cli/src/terminal.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ class ExitCalled extends Error {
5454
}
5555

5656
describe('API errors at the terminal', () => {
57+
it('reports CSV file failures as structured JSON instead of crashing', async () => {
58+
const directory = mkdtempSync(join(tmpdir(), 'sim-cli-missing-csv-'))
59+
try {
60+
const { stderr, exitCode } = await runAgainst(
61+
[
62+
'--output',
63+
'json',
64+
'organizations',
65+
'search',
66+
'sources',
67+
'permissions',
68+
'upload',
69+
'source-1',
70+
'--organization',
71+
'org_test',
72+
'--provider',
73+
'gitlab',
74+
'--expected-revision',
75+
'1',
76+
'--user-mapping',
77+
join(directory, 'missing.csv'),
78+
'--project-permissions',
79+
join(directory, 'projects.csv'),
80+
'--yes',
81+
],
82+
Response.json({})
83+
)
84+
expect(exitCode).toBe(1)
85+
expect(JSON.parse(stderr).error.message).toContain('ENOENT')
86+
} finally {
87+
rmSync(directory, { recursive: true, force: true })
88+
}
89+
})
90+
5791
it('names the server request id in JSON output', async () => {
5892
const { stderr, exitCode } = await runAgainst(
5993
['--output', 'json', 'workflows', 'get', 'wf-missing'],

‎packages/sim-cli/src/transfer/permission-csv.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mkdtemp, open, rm, writeFile } from 'node:fs/promises'
33
import { tmpdir } from 'node:os'
44
import { basename, join } from 'node:path'
55
import { addAbortSignal, Readable } from 'node:stream'
6+
import { getErrorMessage } from '@sim/utils/errors'
67
import { CsvError, parse } from 'csv-parse'
78
import { embedStore } from '#sim-cli/embed-context'
89
import { combineSignals, SimApiError } from '#sim-cli/http/client'
@@ -56,7 +57,9 @@ export async function stagePermissionCsvs(
5657
paths: Record<CsvField, string>,
5758
signal?: AbortSignal
5859
) {
59-
const directory = await mkdtemp(join(tmpdir(), 'sim-permissions-'))
60+
const directory = await mkdtemp(join(tmpdir(), 'sim-permissions-')).catch((error: unknown) => {
61+
throw new SimApiError(`Cannot stage permission CSVs: ${getErrorMessage(error)}`, 0)
62+
})
6063
const parts: { field: CsvField; path: string }[] = []
6164
const manifest = {} as Record<CsvField, CsvManifest>
6265
let totalBytes = 0
@@ -199,6 +202,9 @@ export async function stagePermissionCsvs(
199202
return { manifest, parts, dispose }
200203
} catch (error) {
201204
await dispose()
205+
if (error instanceof SimApiError) throw error
206+
if (error instanceof Error && 'code' in error && typeof error.code === 'string')
207+
throw new SimApiError(`Cannot stage permission CSVs: ${error.message}`, 0)
202208
throw error
203209
}
204210
}

0 commit comments

Comments
 (0)