-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import type { Response } from 'node-fetch' | ||
|
||
export const handleRepsonse = async <T>(method: () => Promise<Response>): Promise<T> => { | ||
try { | ||
const res = await method() | ||
if (res.status < 300) { | ||
if (res.headers.get('content-type')?.startsWith('application/json')) | ||
return await res.json() | ||
else return true as T | ||
} else { | ||
let error: any | ||
try { | ||
error = (await res.json()).error | ||
} catch { | ||
error = { status: res.status, statusText: res.statusText } | ||
} | ||
throw error | ||
const res = await method() | ||
if (res.status < 300) { | ||
if (res.headers.get('content-type')?.startsWith('application/json')) return await res.json() | ||
else return true as T | ||
} else { | ||
let error | ||
try { | ||
error = (await res.json()).error | ||
} catch { | ||
error = { status: res.status, statusText: res.statusText } | ||
} | ||
} catch (error) { | ||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
<script lang="ts"> | ||
export let error: unknown | ||
function hasKeys<K extends string>(value: object, ...keys: K[]): value is { [key in K]: unknown } { | ||
for (const key of keys) | ||
if (!(key in value)) return false | ||
return true | ||
} | ||
export let error: unknown | ||
function hasKeys<K extends string>( | ||
value: object, | ||
...keys: K[] | ||
): value is { [key in K]: unknown } { | ||
for (const key of keys) if (!(key in value)) return false | ||
return true | ||
} | ||
</script> | ||
|
||
<div> | ||
<h4>Oh No! Something went wrong</h4> | ||
{#if typeof error === 'object' && error !== null && hasKeys(error, 'code')} | ||
{#if typeof error.code == 'string' && error.code.includes('internal')} | ||
<p>Internal Error</p> | ||
{:else} | ||
{#if hasKeys(error, 'message')} | ||
<p>{error.message}</p> | ||
{/if} | ||
<h6>Code: {error.code}</h6> | ||
{/if} | ||
{:else} | ||
<p>Uknown Error</p> | ||
{@debug error} | ||
{/if} | ||
</div> | ||
<h4>Oh No! Something went wrong</h4> | ||
{#if typeof error === 'object' && error !== null && hasKeys(error, 'code')} | ||
{#if typeof error.code == 'string' && error.code.includes('internal')} | ||
<p>Internal Error</p> | ||
{:else} | ||
{#if hasKeys(error, 'message')} | ||
<p>{error.message}</p> | ||
{/if} | ||
<h6>Code: {error.code}</h6> | ||
{/if} | ||
{:else} | ||
<p>Uknown Error</p> | ||
{@debug error} | ||
{/if} | ||
</div> |