Add some error handling

This commit is contained in:
SebClem 2024-08-13 12:02:52 +02:00
parent 5f3c59c572
commit a76b20c199
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
5 changed files with 42 additions and 27 deletions

View File

@ -22,24 +22,19 @@ configRouter.get("/backup", (req, res) => {
configRouter.put("/backup", (req, res) => {
validateBackupConfig(req.body as BackupConfig)
.then(
() => {
return saveBackupConfig(req.body as BackupConfig);
},
(error: ValidationError) => {
res.status(400).json(error.details);
.then(() => {
return saveBackupConfig(req.body as BackupConfig);
})
.then(() => {
res.status(204).send();
})
.catch((error: ValidationError) => {
if (error.details) {
res.status(400).json({ type: "validation", errors: error.details });
} else {
res.status(400).json({ type: "cron", errors: [error.message] });
}
)
.then(
() => {
res.status(204).send();
},
() => {
res.status(400).json({
message: "Fail to init cron, please check cron config",
});
}
);
});
});
configRouter.get("/webdav", (req, res) => {
@ -58,9 +53,9 @@ configRouter.put("/webdav", (req, res) => {
.catch((error: ValidationError) => {
res.status(400);
if (error.details) {
res.json(error.details);
res.json({ type: "validation", errors: error.details });
} else {
res.json(error);
res.json({ type: "validation", errors: error });
}
});
});

View File

@ -26,6 +26,14 @@
{{ CronModeFriendly[cron.mode] }}
<v-spacer></v-spacer>
<v-fade-transition>
<v-chip
v-if="errors.includes(cron.id)"
color="error"
variant="flat"
prepend-icon="mdi-alert"
>
INVALID
</v-chip>
<v-chip
v-if="!expanded && cron.monthDay != undefined"
append-icon="mdi-calendar"
@ -186,7 +194,7 @@ import { ref } from "vue";
const expansionPanelModel = ref(undefined);
defineProps<{ loading: boolean }>();
defineProps<{ loading: boolean, errors: string[] }>();
const backupConfigStore = useBackupConfigStore();
function removeCron(id: string) {

View File

@ -33,6 +33,8 @@
variant="outlined"
color="orange"
min="1"
:loading="loading"
:error-messages="errors.nbrToKeep"
></v-text-field>
</v-col>
</v-row>
@ -66,6 +68,8 @@
variant="outlined"
color="orange"
min="1"
:loading="loading"
:error-messages="errors.nbrToKeep"
></v-text-field>
</v-col>
</v-row>
@ -79,7 +83,7 @@
import { useBackupConfigStore } from "@/store/backupConfig";
import { storeToRefs } from "pinia";
defineProps<{ loading: boolean }>();
defineProps<{ loading: boolean, errors: any }>();
const backupConfigStore = useBackupConfigStore();
const { data } = storeToRefs(backupConfigStore);
</script>

View File

@ -41,6 +41,8 @@
density="compact"
variant="outlined"
color="orange"
:loading="loading"
:error-messages="errors.backupType"
>
</v-select>
</v-col>
@ -65,12 +67,12 @@
</v-row>
<v-row dense>
<v-col>
<BackupConfigAutoBackup :loading="loading"></BackupConfigAutoBackup>
<BackupConfigAutoBackup :loading="loading" :errors="errors.cron"></BackupConfigAutoBackup>
</v-col>
</v-row>
<v-row dense>
<v-col>
<BackupConfigAutoClean :loading="loading"></BackupConfigAutoClean>
<BackupConfigAutoClean :loading="loading" :errors=errors></BackupConfigAutoClean>
</v-col>
</v-row>
<v-row dense>
@ -105,12 +107,13 @@ const backupConfigStore = useBackupConfigStore();
const { data } = storeToRefs(backupConfigStore);
const errors = ref({
nameTemplate: [],
username: [],
password: [],
backupType: [],
nbrToKeep: [],
backupDir: [],
allowSelfSignedCerts: [],
type: [],
customEndpoint: [],
cron: []
});
const emit = defineEmits<{

View File

@ -26,13 +26,18 @@ export function useConfigForm(
.catch(async (reason) => {
if (reason instanceof HTTPError) {
const response = await reason.response.json();
if (Array.isArray(response)) {
for (const elem of response) {
if (response["type"] == "validation") {
for (const elem of response["errors"]) {
errorsRef.value[
elem.context.key as keyof typeof errorsRef.value
] = elem.message;
}
}
else if (response["type"] == "cron") {
for (const elem of response["errors"]) {
errorsRef.value["cron"].push(elem)
}
}
}
loading.value = false;
emit("fail");