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
@@ -1,6 +1,9 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { server } from '@/mocks/server';
import { API_BASE } from '@/features/auth/mocks/fixtures';
import { renderApp, mockAuthenticated } from '@/test/utils';
import { emptyHero, failNextUpload, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers';
import { _resetSetupStatusCache } from '@/router';
@@ -271,3 +274,43 @@ describe('Focus dialog', () => {
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
});
});
describe('Upload problems', () => {
it('refuses a file over 10 MB without sending it', async () => {
await openPage();
const big = new File(['x'], 'groot.jpg', { type: 'image/jpeg' });
Object.defineProperty(big, 'size', { value: 11 * 1024 * 1024 });
await userEvent.upload(screen.getByTestId('hero-upload'), big);
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/maximaal 10 MB|at most 10 MB/);
expect(screen.getByTestId('hero-slides-empty')).toBeInTheDocument();
});
it('explains a 413 of the proxy (no JSON body)', async () => {
await openPage();
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => new HttpResponse('<html>413 Request Entity Too Large</html>', { status: 413 })));
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/maximaal 10 MB|at most 10 MB/);
});
it('asks to try again when the connection breaks off', async () => {
await openPage();
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => HttpResponse.error()));
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/afgebroken|interrupted/);
});
it('says when there are too many uploads in a row', async () => {
await openPage();
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => new HttpResponse(null, { status: 429 })));
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/te veel uploads|Too many uploads/);
});
});
+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 = '';
}