mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 07:45:53 +00:00
29 lines
693 B
TypeScript
29 lines
693 B
TypeScript
import { FetcherError } from '@commerce/utils/errors'
|
|
|
|
export function getError(errors: any[], status: number) {
|
|
errors = errors ?? [{ message: 'Failed to fetch Swell API' }]
|
|
return new FetcherError({ errors, status })
|
|
}
|
|
|
|
export async function getAsyncError(res: Response) {
|
|
const data = await res.json()
|
|
return getError(data.errors, res.status)
|
|
}
|
|
|
|
const handleFetchResponse = async (res: Response) => {
|
|
// if (res.ok) {
|
|
// const { data, errors } = await res.json()
|
|
|
|
// if (errors && errors.length) {
|
|
// throw getError(errors, res.status)
|
|
// }
|
|
|
|
// return data
|
|
// }
|
|
if (res) return res
|
|
|
|
throw await getAsyncError(res)
|
|
}
|
|
|
|
export default handleFetchResponse
|