Initial Post list
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user