From dc448d6538955e97f75cadcbca96b5be58d67f31 Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 15 May 2026 20:19:00 +0500 Subject: [PATCH] feat: improve error messages for user upload size validation --- client/src/entities/product/api/reviews-api.ts | 5 +++++ client/src/features/product-review/ui/ReviewDialog.tsx | 4 +++- client/src/shared/constants/upload-limits.ts | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/client/src/entities/product/api/reviews-api.ts b/client/src/entities/product/api/reviews-api.ts index db57bc5..13bcc6b 100644 --- a/client/src/entities/product/api/reviews-api.ts +++ b/client/src/entities/product/api/reviews-api.ts @@ -1,4 +1,5 @@ import { apiClient } from '@/shared/api/client' +import { OTHER_UPLOAD_MAX_FILE_BYTES, formatOtherUploadMaxSizeHint } from '@/shared/constants/upload-limits' export async function postProductReview( productId: string, @@ -8,6 +9,10 @@ export async function postProductReview( } export async function uploadReviewImage(file: File): Promise<{ url: string }> { + if (file.size > OTHER_UPLOAD_MAX_FILE_BYTES) { + throw new Error(`Файл «${file.name}» слишком большой (максимум ${formatOtherUploadMaxSizeHint()}).`) + } + const fd = new FormData() fd.append('file', file, file.name) const { data } = await apiClient.post<{ url: string }>('reviews/upload-image', fd) diff --git a/client/src/features/product-review/ui/ReviewDialog.tsx b/client/src/features/product-review/ui/ReviewDialog.tsx index 6455689..2f9a01c 100644 --- a/client/src/features/product-review/ui/ReviewDialog.tsx +++ b/client/src/features/product-review/ui/ReviewDialog.tsx @@ -128,7 +128,9 @@ export function ReviewDialog({ )} {uploadError ? ( - Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp. + {uploadError instanceof Error + ? uploadError.message + : 'Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp.'} ) : null} {error ? ( diff --git a/client/src/shared/constants/upload-limits.ts b/client/src/shared/constants/upload-limits.ts index 2792064..fa3076e 100644 --- a/client/src/shared/constants/upload-limits.ts +++ b/client/src/shared/constants/upload-limits.ts @@ -2,6 +2,12 @@ import { ADMIN_UPLOAD_IMAGE_MAX_FILE_BYTES_DEFAULT } from '@shared/constants/upl export const ADMIN_UPLOAD_IMAGE_MAX_BYTES = ADMIN_UPLOAD_IMAGE_MAX_FILE_BYTES_DEFAULT +export const OTHER_UPLOAD_MAX_FILE_BYTES = 2 * 1024 * 1024 // 2 MB + export function formatAdminImageMaxSizeHint(): string { return `${Math.round(ADMIN_UPLOAD_IMAGE_MAX_BYTES / (1024 * 1024))} МБ` } + +export function formatOtherUploadMaxSizeHint(): string { + return `${Math.round(OTHER_UPLOAD_MAX_FILE_BYTES / (1024 * 1024))} МБ` +}