Initial Post list

This commit is contained in:
2024-07-25 20:55:35 +02:00
parent 9a2673aba2
commit bb1e0eebf5
16 changed files with 327 additions and 96 deletions
+17 -10
View File
@@ -1,25 +1,32 @@
import { TFunction } from 'i18next';
/**
* Transform error into i18next spreadable input
* Return translated error
* @param error Error object
* @param context Optional context for translation
* @returns Array to be spread into i18next `t` function
* @param context Optional context
* @param t Optional translation function, defautls to pass through
* @returns Translated error or inputs if t as unspecified
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleError = (error: any, context?: string): [string, { context?: string }] => {
if (!error) return ['', {}];
//eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleError = (
error: any,
context?: string,
t: TFunction<'translation', undefined> | ((..._in: any) => any) = (..._in: any) => _in
): string => {
if (!error) return t('', {});
if (error.code) {
switch (error.code) {
case 'NotFound':
return [error.code, { context: `${error.entity}:${context}` }];
return t(error.code, { context: `${error.entity}:${context}` });
case 'Unauthorized':
return [error.code, { context }];
return t(error.code, { context });
default:
return ['Unknown', { context }];
return t('Unknown', { context });
}
}
return [error?.message ?? 'Unknown', { context }];
return t(error?.message ?? 'Unknown', { context });
};
export default handleError;