From 00fd514da5aeba9cdf9daf617ba314ff99dcf703 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 20 Dec 2021 18:22:10 +0100 Subject: [PATCH] feedback on to big error --- client/src/lib/api.ts | 12 ++++++++++-- client/src/lib/views/Create.svelte | 13 ++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/client/src/lib/api.ts b/client/src/lib/api.ts index d8647fe..e884bcf 100644 --- a/client/src/lib/api.ts +++ b/client/src/lib/api.ts @@ -12,15 +12,23 @@ type CallOptions = { body?: any } +export class PayloadToLargeError extends Error {} + async function call(options: CallOptions) { - return fetch('/api/' + options.url, { + const response = await fetch('/api/' + options.url, { method: options.method, body: options.body === undefined ? undefined : JSON.stringify(options.body), mode: 'cors', headers: { 'Content-Type': 'application/json', }, - }).then((r) => r.json()) + }) + + if (!response.ok) { + if (response.status === 413) throw new PayloadToLargeError() + else throw new Error('API call failed') + } + return response.json() } export async function create(note: Note) { diff --git a/client/src/lib/views/Create.svelte b/client/src/lib/views/Create.svelte index 446b6ed..e29d621 100644 --- a/client/src/lib/views/Create.svelte +++ b/client/src/lib/views/Create.svelte @@ -1,5 +1,5 @@