Translated error messages

This commit is contained in:
Przemek Wiech 2020-09-12 20:04:42 +02:00
parent a886230305
commit 13b147f082
9 changed files with 68 additions and 9 deletions

View File

@ -27,6 +27,7 @@ import {
WikiTreeDataSource,
WikiTreeSourceSpec,
} from './datasource/wikitree';
import {getI18nMessage} from './util/error_i18n';
/** Shows an error message in the middle of the screen. */
function ErrorMessage(props: {message?: string}) {
@ -330,7 +331,7 @@ export class App extends React.Component<RouteComponentProps, {}> {
}),
);
} catch (error) {
this.setError(error.message);
this.setError(getI18nMessage(error, this.context.intl));
}
} else if (
this.state.state === AppState.SHOWING_CHART ||

View File

@ -2,6 +2,7 @@ import {analyticsEvent} from '../util/analytics';
import {convertGedcom, getSoftware, TopolaData} from '../util/gedcom_util';
import {DataSource, DataSourceEnum, SourceSelection} from './data_source';
import {IndiInfo, JsonGedcomData} from 'topola';
import {TopolaError} from '../util/error';
/**
* Returns a valid IndiInfo object, either with the given indi and generation
@ -89,7 +90,10 @@ export async function loadGedcom(
console.warn('Failed to load data from session storage: ' + e);
}
if (!gedcom) {
throw new Error('Error loading data. Please upload your file again.');
throw new TopolaError(
'ERROR_LOADING_UPLOADED_FILE',
'Error loading data. Please upload your file again.',
);
}
return prepareData(gedcom, hash, images);
}

View File

@ -5,6 +5,7 @@ import {Date, DateOrRange, JsonFam, JsonIndi} from 'topola';
import {GedcomData, normalizeGedcom, TopolaData} from '../util/gedcom_util';
import {GedcomEntry} from 'parse-gedcom';
import {InjectedIntl} from 'react-intl';
import {TopolaError} from '../util/error';
/** Prefix for IDs of private individuals. */
export const PRIVATE_ID_PREFIX = '~Private';
@ -172,7 +173,12 @@ async function getRelatives(
handleCors,
);
if (response[0].items === null) {
throw new Error(`WikiTree profile ${keysToFetch[0]} not found.`);
const id = keysToFetch[0];
throw new TopolaError(
'WIKITREE_PROFILE_NOT_FOUND',
`WikiTree profile ${id} not found`,
{id},
);
}
const fetchedResults = response[0].items.map(
(x: {person: Person}) => x.person,
@ -236,8 +242,11 @@ export async function loadWikiTree(
// Fetch the ancestors of the input person and ancestors of his/her spouses.
const firstPerson = await getRelatives([key], handleCors);
if (!firstPerson[0].Name) {
throw new Error(
`WikiTree profile ${key} is not accessible. Try logging in.`,
const id = key;
throw new TopolaError(
'WIKITREE_PROFILE_NOT_ACCESSIBLE',
`WikiTree profile ${id} is not accessible. Try logging in.`,
{id},
);
}
@ -597,7 +606,10 @@ export class WikiTreeDataSource implements DataSource<WikiTreeSourceSpec> {
source: SourceSelection<WikiTreeSourceSpec>,
): Promise<TopolaData> {
if (!source.selection) {
throw new Error('WikiTree id needs to be provided');
throw new TopolaError(
'WIKITREE_ID_NOT_PROVIDED',
'WikiTree id needs to be provided',
);
}
try {
const data = await loadWikiTree(

View File

@ -20,7 +20,14 @@ import './index.css';
import 'semantic-ui-css/semantic.min.css';
import 'canvas-toBlob';
addLocaleData([...locale_de, ...locale_en, ...locale_fr, ...locale_it, ...locale_pl, ...locale_ru]);
addLocaleData([
...locale_de,
...locale_en,
...locale_fr,
...locale_it,
...locale_pl,
...locale_ru,
]);
const messages = {
de: messages_de,

View File

@ -304,7 +304,7 @@ export class TopBar extends React.Component<RouteComponentProps & Props> {
>
<FormattedMessage
id="menu.github"
defaultMessage="Source on GitHub"
defaultMessage="GitHub project"
/>
</Dropdown.Item>
</Dropdown.Menu>

View File

@ -62,5 +62,10 @@
"error.failed_png": "Nie udało się utworzyć pliku PNG. Spróbuj jeszcze raz z mniejszym diagramem lub pobierz plik SVG.",
"error.failed_to_load_file": "Błąd wczytywania pliku",
"error.failed_wikitree_load_more": "Błąd podczas pobierania danych z WikiTree. {error}",
"error.GEDCOM_READ_FAILED": "Błąd wczytywania pliku GEDCOM",
"error.ERROR_LOADING_UPLOADED_FILE": "Błąd wczytywania danych. Otwórz ponownie plik.",
"error.WIKITREE_ID_NOT_PROVIDED": "Identyfikator WikiTree nie został podany",
"error.WIKITREE_PROFILE_NOT_ACCESSIBLE": "Profil WikiTree {id} nie jest dostępny",
"error.WIKITREE_PROFILE_NOT_FOUND": "Profil WikiTree {id} nie istnieje",
"wikitree.private": "Prywatne"
}

10
src/util/error.ts Normal file
View 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
View 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,
);
}

View File

@ -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 {