From e826609f6ec09f88b73c29973ddd2c76022bc593 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 8 May 2024 08:16:53 -0500 Subject: [PATCH] Error handling for user registration --- src/pages/Index.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 27a00cd..631fded 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -2,6 +2,7 @@ import { A } from "@solidjs/router"; import { createSignal, onMount, Show } from "solid-js"; import { backend, UserInfo } from "../utils"; import { Card } from "../components/Card"; +import { AxiosError } from "axios"; export function Index() { @@ -29,11 +30,16 @@ export function Index() { throw new Error("Username is empty"); } - const response = await backend.get("/register"); + const response = await backend.get(`/register?username=${user}`); console.log(response.data); } catch (_e) { - const e = _e as Error; - setError(e.message); + const e = _e as AxiosError<{error: string}>; + + if (e.response && e.response.data && e.response.data.error) { + setError(e.response.data.error); + } else { + setError(e.message); + } } finally { setLoading(false); }