Skip to content

Commit 61c2a89

Browse files
committed
bunch of errorhandling cleanup
1 parent dab1fe5 commit 61c2a89

9 files changed

Lines changed: 44 additions & 29 deletions

File tree

src/lib/components/input/PasswordInput.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
// Password is ok, return the successful validation result from the basic validation step
6666
validationResult = { valid: true };
6767
}
68-
} catch (e) {
68+
} catch (error) {
6969
// Show an error toast
70-
await handleApiError(e);
70+
await handleApiError(error);
7171
7272
// We shouldnt block the user from submitting the form if the pwned password check fails
7373
validationResult = { valid: true };

src/lib/components/input/UsernameInput.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
5555
// Map the response to a validation result
5656
validationResult = mapUsernameAvailability(response.availability);
57-
} catch (e) {
57+
} catch (error) {
5858
// Show an error toast
59-
await handleApiError(e);
59+
await handleApiError(error);
6060
6161
// Set the validation result to the internal server error state
6262
validationResult = UsernameInternalServerErrorValRes;

src/lib/errorhandling/apiErrorHandling.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function handleResponseError(
5555
console.trace();
5656
console.groupEnd();
5757

58-
if (handleProblemCallback && !handleProblemCallback(problem)) return;
58+
if (handleProblemCallback && handleProblemCallback(problem)) return;
5959

6060
toast.error(problem.title);
6161
}
@@ -97,3 +97,6 @@ export async function handleApiError(
9797

9898
toast.error('Internal error occured');
9999
}
100+
export function createApiErrorHandler(handleProblemCallback: HandleProblemCallback) {
101+
return (error: unknown) => handleApiError(error, handleProblemCallback);
102+
}

src/lib/stores/UserStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ async function refreshSelf(): Promise<boolean> {
6464
} catch (error) {
6565
reset();
6666

67-
handleApiError(error, (problem) => problem.type !== 'Authentication.CookieMissingOrInvalid');
67+
await handleApiError(
68+
error,
69+
(problem) => problem.type === 'Authentication.CookieMissingOrInvalid'
70+
);
6871

6972
return false;
7073
}

src/routes/(anonymous)/login/+page.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
await UserStore.refreshSelf();
4040
goto(page.url.searchParams.get('redirect') ?? '/home');
4141
} catch (error) {
42-
handleApiError(error, (problem) => {
43-
if (!isValidationError(problem)) return true;
42+
await handleApiError(error, (problem) => {
43+
if (!isValidationError(problem)) return false;
4444
usernameError = mapToValRes(problem, 'UsernameOrEmail');
4545
passwordError = mapToValRes(problem, 'Password');
46-
return false;
46+
return true;
4747
});
4848
}
4949
}

src/routes/(authenticated)/hubs/dialog-hub-regenerate-token.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
loading = true;
2121
try {
2222
newToken = await hubManagementV1Api.devicesRegenerateDeviceToken(hub.id);
23-
} catch (e) {
24-
handleApiError(e);
23+
} catch (error) {
24+
await handleApiError(error);
2525
} finally {
2626
loading = false;
2727
}

src/routes/(authenticated)/settings/account/ChangeUsername.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
username = '';
2727
} catch (e) {
2828
await handleApiError(e, (problem) => {
29-
if (problem.type === 'Account.Username.Invalid') {
30-
toast.error('Invalid Username');
31-
return true;
32-
}
33-
return false;
29+
if (problem.type !== 'Account.Username.Invalid') return false;
30+
31+
toast.error('Invalid Username');
32+
33+
return true;
3434
});
3535
}
3636
}

src/routes/(authenticated)/settings/sessions/+page.svelte

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import Button from '$lib/components/ui/button/button.svelte';
1717
import * as Card from '$lib/components/ui/card';
1818
import { renderComponent } from '$lib/components/ui/data-table';
19+
import type { ProblemDetails } from '$lib/errorhandling/ProblemDetails';
1920
import { handleApiError } from '$lib/errorhandling/apiErrorHandling';
2021
import { onMount } from 'svelte';
2122
import { toast } from 'svelte-sonner';
2223
import DataTableActions from './data-table-actions.svelte';
2324
25+
let loading = $state<boolean>(false);
2426
let data = $state<LoginSessionResponse[]>([]);
2527
let sorting = $state<SortingState>([]);
2628
@@ -43,11 +45,24 @@
4345
},
4446
];
4547
46-
function fetchSessions() {
47-
sessionsApi
48-
.sessionsListSessions()
49-
.then((res) => (data = res))
50-
.catch(handleApiError);
48+
function handleProblem(problem: ProblemDetails): boolean {
49+
return false;
50+
}
51+
52+
async function fetchSessions() {
53+
loading = true;
54+
try {
55+
data = await sessionsApi.sessionsListSessions();
56+
} catch (error) {
57+
await handleApiError(error, handleProblem);
58+
} finally {
59+
loading = false;
60+
}
61+
}
62+
63+
async function onRefreshClicked() {
64+
await fetchSessions();
65+
toast.success('Sessions refreshed successfully');
5166
}
5267
5368
onMount(() => {
@@ -68,13 +83,7 @@
6883
<Card.Header class="w-full">
6984
<Card.Title class="flex items-center justify-between space-x-2 text-3xl">
7085
Sessions
71-
<Button
72-
class="text-xl"
73-
onclick={() => {
74-
fetchSessions();
75-
toast.success('Sessions refreshed successfully');
76-
}}
77-
>
86+
<Button class="text-xl" onclick={onRefreshClicked}>
7887
<RotateCcw />
7988
<span> Refresh </span>
8089
</Button>

src/routes/report/api-tokens/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
await apiTokensApi.tokensReportTokens({ turnstileResponse, secrets });
3131
goto('/login');
3232
} catch (err) {
33-
handleApiError(err);
33+
await handleApiError(err);
3434
}
3535
}
3636

0 commit comments

Comments
 (0)