From 5a0cef7fb97299c7788c2af2ff32529e2bf22e08 Mon Sep 17 00:00:00 2001 From: alannnc Date: Wed, 20 Jul 2022 20:02:00 +0000 Subject: [PATCH] fix/error-handling-with-proper-status (#3453) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add conditional on error handling * Fix return type for handleError Co-authored-by: Omar López --- packages/lib/errors.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/lib/errors.ts b/packages/lib/errors.ts index c6731699be..6bec1e8a50 100644 --- a/packages/lib/errors.ts +++ b/packages/lib/errors.ts @@ -11,7 +11,10 @@ export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: numb } export function handleErrorsJson(response: Response) { - if (!response.ok) { + if (response.status === 204) { + return new Promise((resolve) => resolve({})); + } + if (!response.ok && response.status < 200 && response.status >= 300) { response.json().then(console.log); throw Error(response.statusText); } @@ -19,7 +22,10 @@ export function handleErrorsJson(response: Response) { } export function handleErrorsRaw(response: Response) { - if (!response.ok) { + if (response.status === 204) { + return {}; + } + if (!response.ok && response.status < 200 && response.status >= 300) { response.text().then(console.log); throw Error(response.statusText); }