show invalid fields an messages on action error popup input error
All checks were successful
deploy / build-and-deploy (push) Successful in 18s

This commit is contained in:
2025-05-18 18:26:20 +02:00
parent a83ba25698
commit bb010952a8
2 changed files with 34 additions and 18 deletions

View File

@ -1,10 +1,35 @@
import type { ActionError } from 'astro:actions';
import { type ActionError, isInputError } from 'astro:actions';
import { popupState } from '@components/popup/Popup.ts';
export function actionErrorPopup<E extends Record<string, any>>(error: ActionError<E>) {
export function actionErrorPopup<E extends Record<string, any>>(
error: ActionError<E>,
values?: { title?: string; message?: string }
) {
let title = values?.title;
if (title == undefined) {
if (isInputError(error)) {
title = 'Fehler (ungültige Eingabe)';
} else {
title = `Fehler (${error.status})`;
}
}
let message = values?.message;
if (message == undefined) {
if (isInputError(error)) {
const messages = [];
for (const [name, msgs] of Object.entries(error.fields)) {
messages.push(`${name}: ${(msgs as string[]).join(', ')}`);
}
message = messages.join('\n');
} else {
message = error.message;
}
}
popupState.set({
type: 'error',
title: `Fehler (${error.status})`,
message: error.message
title: title,
message: message
});
}