Skip to content

Commit

Permalink
Merge pull request #563 from toonvanstrijp/feat/error
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Sep 15, 2023
2 parents daaeef8 + c188621 commit 0efe9a9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/decorators/i18n.decorator.ts
@@ -1,5 +1,6 @@
import { createParamDecorator } from '@nestjs/common';
import { I18nContext, logger } from '..';
import { I18nError } from '../i18n.error';

export const I18n = createParamDecorator((_, context) => {
const i18n = I18nContext.current(context);
Expand All @@ -10,7 +11,7 @@ export const I18n = createParamDecorator((_, context) => {
'I18n context not found! Is this function triggered by a processor or cronjob? Please use the I18nService',
);
}
throw Error('I18n context undefined');
throw new I18nError('I18n context undefined');
}

return i18n;
Expand Down
6 changes: 6 additions & 0 deletions src/i18n.error.ts
@@ -0,0 +1,6 @@
export class I18nError extends Error {
constructor(message: string) {
super(message);
this.name = 'I18nError';
}
}
5 changes: 3 additions & 2 deletions src/loaders/i18n.abstract.loader.ts
Expand Up @@ -13,6 +13,7 @@ import {
switchMap,
} from 'rxjs';
import * as chokidar from 'chokidar';
import { I18nError } from '../i18n.error';

export interface I18nAbstractLoaderOptions {
path: string;
Expand Down Expand Up @@ -82,11 +83,11 @@ export abstract class I18nAbstractLoader
const translations: I18nTranslation = {};

if (!(await exists(i18nPath))) {
throw new Error(`i18n path (${i18nPath}) cannot be found`);
throw new I18nError(`i18n path (${i18nPath}) cannot be found`);
}

if (!this.options.filePattern.match(/\*\.[A-z]+/)) {
throw new Error(
throw new I18nError(
`filePattern should be formatted like: *.json, *.txt, *.custom etc`,
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/loaders/i18n.json.loader.ts
@@ -1,3 +1,4 @@
import { I18nError } from '../i18n.error';
import {
I18nAbstractLoader,
I18nAbstractLoaderOptions,
Expand All @@ -15,7 +16,9 @@ export class I18nJsonLoader extends I18nAbstractLoader {
return JSON.parse(data);
} catch (e) {
if (e instanceof SyntaxError) {
throw new Error('Invalid JSON file. Please check your JSON syntax.');
throw new I18nError(
'Invalid JSON file. Please check your JSON syntax.',
);
}
throw e;
}
Expand Down
5 changes: 4 additions & 1 deletion src/loaders/i18n.yaml.loader.ts
@@ -1,3 +1,4 @@
import { I18nError } from '../i18n.error';
import {
I18nAbstractLoader,
I18nAbstractLoaderOptions,
Expand All @@ -17,7 +18,9 @@ export class I18nYamlLoader extends I18nAbstractLoader {
return yaml.load(data, { json: true });
} catch (e) {
if (e instanceof yaml.YAMLException) {
throw new Error('Invalid YAML file. Please check your YAML syntax.');
throw new I18nError(
'Invalid YAML file. Please check your YAML syntax.',
);
}

throw e;
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/i18n.middleware.ts
Expand Up @@ -18,8 +18,9 @@ import {
} from '../index';
import { I18nService } from '../services/i18n.service';
import { I18nOptionResolver } from '../interfaces';
import { I18nError } from '../i18n.error';

const ExecutionContextMethodNotImplemented = new Error(
const ExecutionContextMethodNotImplemented = new I18nError(
"Method not implemented. nestjs-i18n creates a fake Http context since it's using middleware to resolve your language. Nestjs middlewares don't have access to the ExecutionContext.",
);

Expand Down
3 changes: 2 additions & 1 deletion src/services/i18n.service.ts
Expand Up @@ -20,6 +20,7 @@ import { I18nLoader } from '../loaders/i18n.loader';
import { IfAnyOrNever, Path, PathValue } from '../types';
import { formatI18nErrors } from '../utils';
import { I18nTranslator, I18nPluralObject } from '../interfaces';
import { I18nError } from '../i18n.error';

const pluralKeys = ['zero', 'one', 'two', 'few', 'many', 'other'];

Expand Down Expand Up @@ -116,7 +117,7 @@ export class I18nService<K = Record<string, unknown>>
}" in "${lang}" does not exist.`;
this.logger.error(message);
if (this.i18nOptions.throwOnMissingKey) {
throw new Error(message);
throw new I18nError(message);
}
}

Expand Down

0 comments on commit 0efe9a9

Please sign in to comment.