Skip to content

Commit

Permalink
feat: refactor _handleRequest (#708)
Browse files Browse the repository at this point in the history
`_handleRequest` used a Promise instead of async/await which made traces
difficult to debug. Additionally, it handles direct fetch errors which
are always indicative of a network error, CORS or other such issues and
propagtes the error directly.
  • Loading branch information
hf authored Jun 27, 2023
1 parent add762e commit 65f1c52
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
const _getErrorMessage = (err: any): string =>
err.msg || err.message || err.error_description || err.error || JSON.stringify(err)

const handleError = async (error: unknown, reject: (reason?: any) => void) => {
const NETWORK_ERROR_CODES = [502, 503, 504]
const NETWORK_ERROR_CODES = [502, 503, 504]

async function handleError(error: unknown) {
if (!looksLikeFetchResponse(error)) {
reject(new AuthRetryableFetchError(_getErrorMessage(error), 0))
} else if (NETWORK_ERROR_CODES.includes(error.status)) {
throw new AuthRetryableFetchError(_getErrorMessage(error), 0)
}

if (NETWORK_ERROR_CODES.includes(error.status)) {
// status in 500...599 range - server had an error, request might be retryed.
reject(new AuthRetryableFetchError(_getErrorMessage(error), error.status))
} else {
// got a response from server that is not in the 500...599 range - should not retry
error
.json()
.then((err) => {
reject(new AuthApiError(_getErrorMessage(err), error.status || 500))
})
.catch((e) => {
// not a valid json response
reject(new AuthUnknownError(_getErrorMessage(e), e))
})
throw new AuthRetryableFetchError(_getErrorMessage(error), error.status)
}

let data: any
try {
data = await error.json()
} catch (e: any) {
throw new AuthUnknownError(_getErrorMessage(e), e)
}

throw new AuthApiError(_getErrorMessage(data), error.status || 500)
}

const _getRequestParams = (
Expand Down Expand Up @@ -110,16 +111,32 @@ async function _handleRequest(
parameters?: FetchParameters,
body?: object
): Promise<any> {
return new Promise((resolve, reject) => {
fetcher(url, _getRequestParams(method, options, parameters, body))
.then((result) => {
if (!result.ok) throw result
if (options?.noResolveJson) return result
return result.json()
})
.then((data) => resolve(data))
.catch((error) => handleError(error, reject))
})
const requestParams = _getRequestParams(method, options, parameters, body)

let result: any

try {
result = await fetcher(url, requestParams)
} catch (e) {
console.error(e)

// fetch failed, likely due to a network or CORS error
throw new AuthRetryableFetchError(_getErrorMessage(e), 0)
}

if (!result.ok) {
await handleError(result)
}

if (options?.noResolveJson) {
return result
}

try {
return await result.json()
} catch (e: any) {
await handleError(e)
}
}

export function _sessionResponse(data: any): AuthResponse {
Expand Down

0 comments on commit 65f1c52

Please sign in to comment.