From 99d42f5125d23cb9e6251fa81881e4536d175e1c Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 6 Jul 2023 15:37:22 +0800 Subject: [PATCH] Improved APIs to respond to unreachable error --- src/components/Api/Api.tsx | 22 ++++++++++++++++------ src/routes/Register/Register.tsx | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/components/Api/Api.tsx b/src/components/Api/Api.tsx index 8820598..75300cf 100644 --- a/src/components/Api/Api.tsx +++ b/src/components/Api/Api.tsx @@ -51,7 +51,10 @@ export function UserRegister(register: RegistrationParams) { return [true, response.status]; }) .catch((error) => { - return [false, error.response.status, error.response.data]; + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + return [false, error_message]; }); } @@ -70,8 +73,10 @@ export function UserLogin(user: LoginParams) { return [true]; }) .catch((error) => { - console.log("Login Failed:" + JSON.stringify(error.response.data)); - return [false, error.response.data]; + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + return [false, error_message]; }); } @@ -91,7 +96,10 @@ export async function TokenRefresh() { return true; }) .catch((error) => { - console.log("Refresh Failed: " + JSON.stringify(error.response)); + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + console.log("Token Refresh error:", error_message); return false; }); } @@ -108,8 +116,10 @@ export async function UserInfo() { return response.data; }) .catch((error) => { - console.log("User Info Error", error.response.data); - return [false, error.response.data]; + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + return [false, error_message]; }); } diff --git a/src/routes/Register/Register.tsx b/src/routes/Register/Register.tsx index f484f11..ea2b90f 100644 --- a/src/routes/Register/Register.tsx +++ b/src/routes/Register/Register.tsx @@ -159,7 +159,7 @@ export default function Register() { } else { setUser({ ...user, - feedback: ParseError(JSON.stringify(result[2])), + feedback: ParseError(JSON.stringify(result[1])), }); } });