import-csv: improve error handling (hide 500, add close button)

This commit is contained in:
josse 2025-10-02 09:39:54 +02:00
parent 0986af4f81
commit 0ee873d8b2
4 changed files with 68 additions and 22 deletions

View file

@ -26,7 +26,7 @@ GitHub's online interface.
For users who previously confirmed the license of their contributions on the For users who previously confirmed the license of their contributions on the
support site, it would be great if you could add your name below as well. support site, it would be great if you could add your name below as well.
******************** ---
AMBOSS MD Inc. <https://www.amboss.com/> AMBOSS MD Inc. <https://www.amboss.com/>
Aristotelis P. <https://glutanimate.com/contact> Aristotelis P. <https://glutanimate.com/contact>
@ -247,8 +247,9 @@ Hanna Nilsén <hanni614@student.liu.se>
Elias Johansson Lara <elias.johanssonlara@gmail.com> Elias Johansson Lara <elias.johanssonlara@gmail.com>
Toby Penner <tobypenner01@gmail.com> Toby Penner <tobypenner01@gmail.com>
Danilo Spillebeen <spillebeendanilo@gmail.com> Danilo Spillebeen <spillebeendanilo@gmail.com>
Josefin Odermalm <112946011+josod827@users.noreply.github.com>
******************** ---
The text of the 3 clause BSD license follows: The text of the 3 clause BSD license follows:
@ -258,15 +259,15 @@ Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this 1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, 2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors 3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without may be used to endorse or promote products derived from this software without
specific prior written permission. specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

View file

@ -2,12 +2,32 @@
Copyright: Ankitects Pty Ltd and contributors Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script context="module" lang="ts">
declare function pycmd(cmd: string): void;
</script>
<script lang="ts"> <script lang="ts">
export let error: Error; export let error: Error;
function normalizeErrorMessage(message: string): string {
return message.replace(/^\d{3}:\s*/, "");
}
function closeWindow() {
try {
pycmd("close");
} catch {
history.back();
}
}
</script> </script>
<div class="message"> <div class="message">
{error.message} {normalizeErrorMessage(error.message)}
</div>
<div class="actions">
<button on:click={closeWindow}>Close</button>
</div> </div>
<style lang="scss"> <style lang="scss">
@ -15,4 +35,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
text-align: center; text-align: center;
margin: 50px 0 0; margin: 50px 0 0;
} }
.actions {
text-align: center;
margin-top: 1rem;
button {
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
background: var(--anki-button-bg, #007acc);
color: white;
cursor: pointer;
}
}
</style> </style>

View file

@ -4,9 +4,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script lang="ts"> <script lang="ts">
import ImportCsvPage from "../ImportCsvPage.svelte"; import ImportCsvPage from "../ImportCsvPage.svelte";
import ErrorPage from "$lib/components/ErrorPage.svelte";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
export let data: PageData; export let data: PageData;
</script> </script>
<ImportCsvPage state={data.state} /> {#if data.initialError}
<ErrorPage error={data.initialError} />
{:else}
<ImportCsvPage state={data.state} />
{/if}

View file

@ -6,6 +6,7 @@ import { ImportCsvState } from "../lib";
import type { PageLoad } from "./$types"; import type { PageLoad } from "./$types";
export const load = (async ({ params }) => { export const load = (async ({ params }) => {
try {
const [notetypes, decks, metadata] = await Promise.all([ const [notetypes, decks, metadata] = await Promise.all([
getNotetypeNames({}), getNotetypeNames({}),
getDeckNames({ getDeckNames({
@ -14,6 +15,11 @@ export const load = (async ({ params }) => {
}), }),
getCsvMetadata({ path: params.path }, { alertOnError: false }), getCsvMetadata({ path: params.path }, { alertOnError: false }),
]); ]);
const state = new ImportCsvState(params.path, notetypes, decks, metadata); const state = new ImportCsvState(params.path, notetypes, decks, metadata);
return { state }; return { state };
} catch (e: any) {
const rawMsg = e?.message ?? String(e ?? "");
return { initialError: new Error(rawMsg) };
}
}) satisfies PageLoad; }) satisfies PageLoad;