From e8328f94a35c7b03437c015b54b14e68b2136b02 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Wed, 30 Nov 2022 14:37:37 +0100 Subject: [PATCH] feat: typed error handling --- frontend/src/api/index.ts | 12 ++++++++++-- frontend/src/routes/dashboard/+page.svelte | 19 +++++++++++-------- frontend/src/stores/candidate.ts | 1 + 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index fed5732..6c4c32c 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -1,5 +1,13 @@ +import { goto } from "$app/navigation"; +import type { AxiosError } from "axios"; + export const API_URL = "http://localhost:8000"; -export function errorHandler(error: { data?: { [key: string]: string[] } }, fallbackMessage: string) { - return error?.data?.errors ? error.data.errors : { unknown: [fallbackMessage] }; +export interface ApiError { + error: AxiosError, + msg: string, +} + +export function errorHandler(error: AxiosError, msg: string): ApiError { + return {error, msg} } \ No newline at end of file diff --git a/frontend/src/routes/dashboard/+page.svelte b/frontend/src/routes/dashboard/+page.svelte index 1a1201a..7dcb68f 100644 --- a/frontend/src/routes/dashboard/+page.svelte +++ b/frontend/src/routes/dashboard/+page.svelte @@ -9,31 +9,34 @@ import { candidateData, fetchDetails } from '../../stores/candidate'; - let name = ""; + let fullName = ""; let email = ""; $: if ($candidateData === undefined) { fetch(); } else { - name = $candidateData.name + " " + $candidateData.surname; - email = $candidateData.email; + setNameEmail(); } const fetch = async () => { try { await fetchDetails(); - name = $candidateData.name + " " + $candidateData.surname; - email = $candidateData.email; + setNameEmail(); } catch { console.error("error"); } } + + const setNameEmail = async () => { + fullName = $candidateData.name + " " + $candidateData.surname; + email = $candidateData.email ?? "!Nevyplněné údaje!"; + }
- + {email} Uchazeč na SSPŠ @@ -50,8 +53,8 @@
- - {name} + + {fullName} Uchazeč na SSPŠ
diff --git a/frontend/src/stores/candidate.ts b/frontend/src/stores/candidate.ts index 27591c0..51455d9 100644 --- a/frontend/src/stores/candidate.ts +++ b/frontend/src/stores/candidate.ts @@ -1,5 +1,6 @@ import { apiFetchDetails, apiFillDetails, apiLogin, apiLogout } from "../api/candidate"; import { writable } from "svelte/store"; +import type { ApiError } from "src/api"; export interface CandidateData { name?: string;