diff options
Diffstat (limited to 'packages/db/src/runtime/utils.ts')
-rw-r--r-- | packages/db/src/runtime/utils.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/packages/db/src/runtime/utils.ts b/packages/db/src/runtime/utils.ts new file mode 100644 index 000000000..32bc60a45 --- /dev/null +++ b/packages/db/src/runtime/utils.ts @@ -0,0 +1,18 @@ +/** + * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback. + */ +export async function safeFetch( + url: Parameters<typeof fetch>[0], + options: Parameters<typeof fetch>[1] = {}, + onNotOK: (response: Response) => void | Promise<void> = () => { + throw new Error(`Request to ${url} returned a non-OK status code.`); + } +): Promise<Response> { + const response = await fetch(url, options); + + if (!response.ok) { + await onNotOK(response); + } + + return response; +}
\ No newline at end of file |