feat: typed error handling

This commit is contained in:
Sebastian Pravda 2022-11-30 14:37:37 +01:00 committed by EETagent
parent ed51ae97d1
commit e8328f94a3
3 changed files with 22 additions and 10 deletions

View file

@ -1,5 +1,13 @@
import { goto } from "$app/navigation";
import type { AxiosError } from "axios";
export const API_URL = "http://localhost:8000"; export const API_URL = "http://localhost:8000";
export function errorHandler(error: { data?: { [key: string]: string[] } }, fallbackMessage: string) { export interface ApiError {
return error?.data?.errors ? error.data.errors : { unknown: [fallbackMessage] }; error: AxiosError,
msg: string,
}
export function errorHandler(error: AxiosError, msg: string): ApiError {
return {error, msg}
} }

View file

@ -9,31 +9,34 @@
import { candidateData, fetchDetails } from '../../stores/candidate'; import { candidateData, fetchDetails } from '../../stores/candidate';
let name = ""; let fullName = "";
let email = ""; let email = "";
$: if ($candidateData === undefined) { $: if ($candidateData === undefined) {
fetch(); fetch();
} else { } else {
name = $candidateData.name + " " + $candidateData.surname; setNameEmail();
email = $candidateData.email;
} }
const fetch = async () => { const fetch = async () => {
try { try {
await fetchDetails(); await fetchDetails();
name = $candidateData.name + " " + $candidateData.surname; setNameEmail();
email = $candidateData.email;
} catch { } catch {
console.error("error"); console.error("error");
} }
} }
const setNameEmail = async () => {
fullName = $candidateData.name + " " + $candidateData.surname;
email = $candidateData.email ?? "!Nevyplněné údaje!";
}
</script> </script>
<FullLayout> <FullLayout>
<div class="dashboard dashboardDesktop"> <div class="dashboard dashboardDesktop">
<div class="name col-span-3"> <div class="name col-span-3">
<DashboardInfoCard title={name}> <DashboardInfoCard title={fullName}>
<span class="mt-3 text-sspsBlue truncate">{email}</span> <span class="mt-3 text-sspsBlue truncate">{email}</span>
<span class="mt-3 text-sspsGray text-xs">Uchazeč na SSPŠ</span> <span class="mt-3 text-sspsGray text-xs">Uchazeč na SSPŠ</span>
</DashboardInfoCard> </DashboardInfoCard>
@ -50,8 +53,8 @@
</div> </div>
<div class="dashboard dashboardMobile"> <div class="dashboard dashboardMobile">
<div class="my-10 name w-[90%] mx-auto"> <div class="my-10 name w-[90%] mx-auto">
<DashboardInfoCard title={name}> <DashboardInfoCard title={fullName}>
<span class="mt-3 text-sspsBlue truncate">{name}</span> <span class="mt-3 text-sspsBlue truncate">{fullName}</span>
<span class="mt-3 text-sspsGray text-xs">Uchazeč na SSPŠ</span> <span class="mt-3 text-sspsGray text-xs">Uchazeč na SSPŠ</span>
</DashboardInfoCard> </DashboardInfoCard>
</div> </div>

View file

@ -1,5 +1,6 @@
import { apiFetchDetails, apiFillDetails, apiLogin, apiLogout } from "../api/candidate"; import { apiFetchDetails, apiFillDetails, apiLogin, apiLogout } from "../api/candidate";
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import type { ApiError } from "src/api";
export interface CandidateData { export interface CandidateData {
name?: string; name?: string;