From 862b5074a404b3a20c73787fdd8020cce0ed4a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Bj=C3=B6rklund?= Date: Fri, 24 May 2024 22:48:46 +0200 Subject: [PATCH] Update client.ts with better error This update will fix the problem that many users has about Runtime error when the user already exists. Now the error will return "User already exists" as it should. --- src/client.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index 33a275e..8bd0859 100644 --- a/src/client.ts +++ b/src/client.ts @@ -17,13 +17,13 @@ export const signUp = async (payload: SignUpPayload): Promise => { }); if (!res.ok) { - const isJson = res.headers - .get('Content-Type') - ?.includes('application/json'); - + const isJson = (_a = res.headers.get('Content-Type')) === null || _a === void 0 ? void 0 : _a.includes('application/json'); const data = isJson ? await res.json() : await res.text(); - - throw new Error(data); + + // Check if the data is JSON and has an 'error' key, then use it as the error message. + const errorMessage = isJson && typeof data === 'object' && data.error ? data.error : 'Unknown error occurred'; + + throw new Error(errorMessage); } const user = await res.json();