Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor _handleRequest #708

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Changes from all commits
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
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