Skip to content

Commit e4bf9fe

Browse files
committed
feat: add missing error messages and translations
1 parent 4a6b24b commit e4bf9fe

File tree

7 files changed

+73
-10
lines changed

7 files changed

+73
-10
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ S3_SECRET_ACCESS_KEY="startui-secret-key"
1919
S3_REGION="default"
2020

2121
# PUBLIC CONFIG
22-
VITE_S3_BUCKET_PUBLIC_URL="http://127.0.0.1:${DOCKER_MINIO_API_PORT}/${S3_BUCKET_NAME}"
22+
VITE_S3_BUCKET_PUBLIC_URL="${S3_ENDPOINT}/${S3_BUCKET_NAME}"
2323
VITE_BASE_URL="http://localhost:${VITE_PORT}"
2424
# Use the following environment variables to show the environment name.
2525
VITE_ENV_NAME="LOCAL"

src/features/book/manager/form-book-cover.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { useUploadFile } from 'better-upload/client';
33
import { useFormContext, useWatch } from 'react-hook-form';
4+
import { useTranslation } from 'react-i18next';
5+
import { toast } from 'sonner';
46

57
import { orpc } from '@/lib/orpc/client';
68

79
import { BookCover } from '@/features/book/book-cover';
810
import { FormFieldsBook } from '@/features/book/schema';
911

1012
export const FormBookCover = () => {
13+
const { t } = useTranslation(['book']);
1114
const form = useFormContext<FormFieldsBook>();
1215
const genresQuery = useQuery(orpc.genre.getAll.queryOptions());
1316
const title = useWatch({
@@ -34,10 +37,17 @@ export const FormBookCover = () => {
3437
onUploadComplete: ({ file }) => {
3538
form.setValue('coverId', file.objectKey);
3639
},
40+
onError: (error) => {
41+
if (error.type === 'rejected') {
42+
// In this specific case, error should be a translated message
43+
// because rejected are custom errors thrown by the developper
44+
toast.error(error.message);
45+
} else {
46+
toast.error(t(`book:manager.uploadErrors.${error.type}`));
47+
}
48+
},
3749
});
3850

39-
// [TODO] Handle upload errors
40-
4151
return (
4252
<div className="relative">
4353
<label htmlFor="coverId">

src/locales/ar/book.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@
5858
"label": "حفظ"
5959
},
6060
"updateError": "فشل في تحديث الكتاب"
61+
},
62+
"uploadErrors": {
63+
"NOT_AUTHENTICATED": "تحتاج إلى تسجيل الدخول لتتمكن من رفع غلاف جديد",
64+
"UNAUTHORIZED": "غير مسموح لك بتغيير غلاف الكتاب",
65+
"unknown": "حدث خطأ غير معروف أثناء الرفع",
66+
"invalid_request": "طلب رفع غير صالح",
67+
"no_files": "لم يتم اختيار ملفات للرفع",
68+
"s3_upload": "فشل في رفع الملف إلى التخزين",
69+
"file_too_large": "الملف كبير جداً. الحد الأقصى للحجم 100 ميجابايت",
70+
"invalid_file_type": "نوع الملف غير صالح. مسموح فقط بصور PNG و JPEG و WebP و GIF",
71+
"rejected": "تم رفض الرفع",
72+
"too_many_files": "تم اختيار عدد كبير من الملفات",
73+
"aborted": "تم إلغاء الرفع"
6174
}
6275
}
6376
}

src/locales/en/book.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,20 @@
5757
"updateButton": {
5858
"label": "Save"
5959
},
60-
"updateError": "Failed to update a book",
61-
"uploadErrorCodes": {
62-
"NOT_AUTHENTICATED": "You need to be authenticated to be able to upload a new cover",
63-
"UNAUTHORIZED": "You are not allowed to change book cover"
64-
}
60+
"updateError": "Failed to update a book"
61+
},
62+
"uploadErrors": {
63+
"NOT_AUTHENTICATED": "You need to be authenticated to be able to upload a new cover",
64+
"UNAUTHORIZED": "You are not allowed to change book cover",
65+
"unknown": "An unknown error occurred during upload",
66+
"invalid_request": "Invalid upload request",
67+
"no_files": "No files selected for upload",
68+
"s3_upload": "Failed to upload file to storage",
69+
"file_too_large": "File is too large. Maximum size is 100MB",
70+
"invalid_file_type": "Invalid file type. Only PNG, JPEG, WebP, and GIF images are allowed",
71+
"rejected": "Upload was rejected",
72+
"too_many_files": "Too many files selected",
73+
"aborted": "Upload was cancelled"
6574
}
6675
}
6776
}

src/locales/fr/book.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@
5858
"label": "Sauvegarder"
5959
},
6060
"updateError": "Échec de la modification du livre"
61+
},
62+
"uploadErrors": {
63+
"NOT_AUTHENTICATED": "Vous devez être authentifié pour pouvoir télécharger une nouvelle couverture",
64+
"UNAUTHORIZED": "Vous n'êtes pas autorisé à changer la couverture du livre",
65+
"unknown": "Une erreur inconnue s'est produite lors du téléchargement",
66+
"invalid_request": "Demande de téléchargement invalide",
67+
"no_files": "Aucun fichier sélectionné pour le téléchargement",
68+
"s3_upload": "Échec du téléchargement du fichier vers le stockage",
69+
"file_too_large": "Le fichier est trop volumineux. Taille maximale : 100 Mo",
70+
"invalid_file_type": "Type de fichier invalide. Seules les images PNG, JPEG, WebP et GIF sont autorisées",
71+
"rejected": "Le téléchargement a été rejeté",
72+
"too_many_files": "Trop de fichiers sélectionnés",
73+
"aborted": "Le téléchargement a été annulé"
6174
}
6275
}
6376
}

src/locales/sw/book.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@
5858
"label": "Hifadhi"
5959
},
6060
"updateError": "Imeshindikana kusasisha kitabu"
61+
},
62+
"uploadErrors": {
63+
"NOT_AUTHENTICATED": "Unahitaji kuwa umeingia ili uweze kupakia jalada jipya",
64+
"UNAUTHORIZED": "Hauruhusiwi kubadilisha jalada la kitabu",
65+
"unknown": "Hitilafu isiyojulikana imetokea wakati wa kupakia",
66+
"invalid_request": "Ombi la kupakia si halali",
67+
"no_files": "Hakuna faili zilizochaguliwa kwa kupakia",
68+
"s3_upload": "Imeshindwa kupakia faili kwenye hifadhi",
69+
"file_too_large": "Faili ni kubwa sana. Ukubwa wa juu ni 100MB",
70+
"invalid_file_type": "Aina ya faili si halali. Picha za PNG, JPEG, WebP, na GIF tu ndizo zinazoruhusiwa",
71+
"rejected": "Kupakia kumekataliwa",
72+
"too_many_files": "Faili nyingi sana zimechaguliwa",
73+
"aborted": "Kupakia kumeghairiwa"
6174
}
6275
}
6376
}

src/routes/api/upload.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type Router,
77
} from 'better-upload/server';
88

9+
import i18n from '@/lib/i18n';
910
import { s3client } from '@/lib/s3';
1011

1112
import { envServer } from '@/env/server';
@@ -21,7 +22,9 @@ const router: Router = {
2122
onBeforeUpload: async ({ req, file }) => {
2223
const session = await auth.api.getSession({ headers: req.headers });
2324
if (!session?.user) {
24-
throw new RejectUpload('manager.uploadError.NOT_AUTHENTICATED');
25+
throw new RejectUpload(
26+
i18n.t('book:manager.uploadErrors.NOT_AUTHENTICATED')
27+
);
2528
}
2629

2730
// Only admins should be able to update book covers
@@ -36,7 +39,9 @@ const router: Router = {
3639
});
3740

3841
if (!canUpdateBookCover.success) {
39-
throw new RejectUpload('manager.uploadError.UNAUTHORIZED');
42+
throw new RejectUpload(
43+
i18n.t('book:manager.uploadErrors.UNAUTHORIZED')
44+
);
4045
}
4146

4247
// normalize file extension from detected mimetype

0 commit comments

Comments
 (0)