Line data Source code
1 : /**
2 : * The one place a billing HTTP status becomes a code. The transport
3 : * deliberately returns an answer of any status as `ok`, so that every typed
4 : * operation maps status the same way instead of each inventing its own.
5 : */
6 : import type { BillingErrorCode, BillingHttpResponse } from './billingContracts'
7 :
8 0 : export function codeForHttpStatus(
9 : response: Pick<
10 : BillingHttpResponse,
11 : 'httpStatus' | 'authenticationRetrySkipped' | 'authenticationNotRenewable'
12 : >
13 : ): BillingErrorCode {
14 0 : const status = response.httpStatus
15 : // A 401 the transport re-minted and retried is a real refusal. One it had
16 : // to leave alone, because the write was not replayable, says only that the
17 : // token had gone stale; the caller's next attempt mints a fresh one, so it
18 : // joins the transient bucket rather than reading as a permanent denial.
19 0 : if (status === 401 && response.authenticationRetrySkipped) {
20 0 : return 'REQUEST_FAILED'
21 : }
22 : // A 401 on a transport that never minted anything proves nothing about
23 : // permission: the host's credential expired, and signing in again is the
24 : // only recovery. ACCESS_DENIED is the one code that tells a user not to try.
25 0 : if (status === 401 && response.authenticationNotRenewable) {
26 0 : return 'NOT_AUTHENTICATED'
27 : }
28 0 : if (status === 401 || status === 403) return 'ACCESS_DENIED'
29 0 : if (status === 404) return 'NOT_FOUND'
30 0 : if (status === 409) return 'CONFLICT'
31 : // 5xx joins the transient bucket with the network failures the transport
32 : // already reports there: one retryable failure for a caller, not several.
33 0 : return 'REQUEST_FAILED'
34 : }
|