H5: duidelijke meldingen bij een te groot bestand, een 413 van de proxy, een afgebroken of te vaak herhaalde upload

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 16:40:57 +01:00
co-authored by Claude Sonnet 5.5
parent d597faf86a
commit d26720ee75
4 changed files with 79 additions and 2 deletions
+26 -2
View File
@@ -62,6 +62,25 @@ function serverMessage(error: unknown, generic: string, network: string): string
return generic;
}
/** The most the host accepts for one image (Media:MaxUploadBytes). */
const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
/**
* What to tell the administrator when an upload fails: the message of the host when it sent one, a clear text for a
* refusal by the proxy (413, which has no JSON body) or a rate limit, and a retry hint when the connection broke off.
*/
function uploadMessage(error: unknown, t: (key: string, options?: Record<string, unknown>) => string): string {
if (error instanceof ProblemDetailsError) {
const message = error.problem.message;
if (typeof message === 'string' && message !== '') return message;
if (error.status === 413) return t('hero.upload.tooLarge', { max: MAX_UPLOAD_BYTES / (1024 * 1024) });
if (error.status === 429) return t('hero.upload.busy');
return t('errors.generic');
}
if (error instanceof NetworkError) return t('hero.upload.interrupted');
return t('errors.generic');
}
/** The smallest WebP of the wide ratio for a thumbnail; the plain source when there is none. */
function thumbnail(image: HeroImageDto): string {
const small = image.variants
@@ -137,13 +156,18 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
const file = files?.[0];
if (!file) return;
setUploadError(null);
// The host (and the proxy in front of it) refuse bigger files; say so without sending the whole file first.
if (file.size > MAX_UPLOAD_BYTES) {
setUploadError(t('hero.upload.tooLarge', { max: MAX_UPLOAD_BYTES / (1024 * 1024) }));
if (fileInput.current) fileInput.current.value = '';
return;
}
try {
const image = await upload.mutateAsync(file);
setLocalImages((current) => ({ ...current, [image.id]: image }));
slides.append({ id: null, imageId: image.id, alt: '', decorative: false, isActive: true }, { shouldFocus: false });
} catch (error) {
const message = error instanceof ProblemDetailsError ? error.problem.message : undefined;
setUploadError(typeof message === 'string' ? message : error instanceof NetworkError ? t('errors.network') : t('errors.generic'));
setUploadError(uploadMessage(error, t));
} finally {
if (fileInput.current) fileInput.current.value = '';
}