diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-05-04 12:22:25 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-05-04 12:22:25 +0500 |
| commit | b82265994c5f72c40f68c6b3a4960869b6a67951 (patch) | |
| tree | f2e7524ffded1de4bc29d3b4c5c4a7236111129e /front | |
| parent | e02f7ee46876d32ca094c2b5cf3647248baf7645 (diff) | |
Register error handling
Diffstat (limited to 'front')
| -rw-r--r-- | front/src/Authentication/RegisterPage.tsx | 20 | ||||
| -rw-r--r-- | front/src/Models/ErrorResponce.ts | 3 |
2 files changed, 20 insertions, 3 deletions
diff --git a/front/src/Authentication/RegisterPage.tsx b/front/src/Authentication/RegisterPage.tsx index b30b182..5c818f6 100644 --- a/front/src/Authentication/RegisterPage.tsx +++ b/front/src/Authentication/RegisterPage.tsx @@ -1,10 +1,11 @@ -import axios from "axios"; +import axios, { AxiosError } from "axios"; import { useContext, useState } from "react"; import { useNavigate } from "react-router"; import { BackendURL } from "../Config"; import { Authentication, SaveState } from "./ContextProvider"; import { GetLocalizedString } from "../Locales/Locales"; import { LanguageContext } from "../Locales/Context"; +import { BackendError } from "../Models/ErrorResponce"; const RegisterURL = `${BackendURL}/auth/register`; @@ -13,6 +14,7 @@ function RegisterPage() { const [username, setUsername] = useState<string>(""); const [passw, setPassw] = useState<string>(""); + const [errMessage, setErrorMessage] = useState<string>(""); const navigate = useNavigate(); function SetAuthState(newAuthState: Authentication | null) { @@ -37,13 +39,17 @@ function RegisterPage() { SetAuthState(data); navigate("/"); window.location.reload(); + }, (err) => { + setErrorMessage(err); + console.log(err); }); }}> { GetLocalizedString("Register", lang) } </button> + <p> { errMessage } </p> </div> ); } -async function Register(username: string, passw: string, onSuccess: (data: Authentication) => void) { +async function Register(username: string, passw: string, onSuccess: (data: Authentication) => void, onError: (message: string) => void) { await axios.post<Authentication>( RegisterURL, { Username: username, @@ -52,7 +58,15 @@ async function Register(username: string, passw: string, onSuccess: (data: Authe ).then(resp => { onSuccess(resp.data); }).catch(err => { - console.log(`Failed to send a login responce: ${err}`); + if (axios.isAxiosError(err)) { + const parsedErr = err as AxiosError<BackendError>; + if (parsedErr.response) { + console.log(parsedErr.response); + onError(parsedErr.response.data.Message); + return + } + } + return "An unexpected error occured" }); } diff --git a/front/src/Models/ErrorResponce.ts b/front/src/Models/ErrorResponce.ts new file mode 100644 index 0000000..bc0c552 --- /dev/null +++ b/front/src/Models/ErrorResponce.ts @@ -0,0 +1,3 @@ +export type BackendError = { + Message: string, +}; |
