mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-07-24 12:41:50 +00:00
Translated error messages
This commit is contained in:
10
src/util/error.ts
Normal file
10
src/util/error.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/** Error class adding an error code used for i18n. */
|
||||
export class TopolaError extends Error {
|
||||
constructor(
|
||||
public readonly code: string,
|
||||
message: string,
|
||||
public readonly args: {[key: string]: string} = {},
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
19
src/util/error_i18n.ts
Normal file
19
src/util/error_i18n.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {InjectedIntl} from 'react-intl';
|
||||
import {TopolaError} from './error';
|
||||
|
||||
/**
|
||||
* Returns a translated message for the given error. If the message can't be
|
||||
* translated, the original error.message is returned.
|
||||
*/
|
||||
export function getI18nMessage(error: Error, intl: InjectedIntl): string {
|
||||
if (!(error instanceof TopolaError)) {
|
||||
return error.message;
|
||||
}
|
||||
return intl.formatMessage(
|
||||
{
|
||||
id: `error.${error.code}`,
|
||||
defaultMessage: error.message,
|
||||
},
|
||||
error.args,
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import {GedcomEntry, parse as parseGedcom} from 'parse-gedcom';
|
||||
import {TopolaError} from './error';
|
||||
import {
|
||||
JsonFam,
|
||||
JsonGedcomData,
|
||||
@@ -245,7 +246,7 @@ export function convertGedcom(
|
||||
!json.fams ||
|
||||
!json.fams.length
|
||||
) {
|
||||
throw new Error('Failed to read GEDCOM file');
|
||||
throw new TopolaError('GEDCOM_READ_FAILED', 'Failed to read GEDCOM file');
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user