Skip to content

Commit

Permalink
Throw custom errors (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed Oct 16, 2021
1 parent 070f35b commit 275e591
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { Linguini } from './linguini';
export { LangFile, CommonFile, CategoryItems, TypeMapper } from './models/internal-models';
export { Utils } from './utils';
export { TypeMappers } from './type-mappers';
export { LinguiniError } from './models/error-models';
24 changes: 21 additions & 3 deletions src/linguini.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import { LinguiniError } from './models/error-models';
import { CommonFile, LangFile, TypeMapper } from './models/internal-models';
import { DataUtils, FileUtils, RegexUtils } from './utils/';

Expand Down Expand Up @@ -32,8 +33,8 @@ export class Linguini {
* @returns A new Linguini object.
*/
constructor(
private folderPath: string,
private fileName: string,
public folderPath: string,
public fileName: string,
options: Partial<LinguiniOptions> = {}
) {
this.options = Object.assign(this.options, options);
Expand Down Expand Up @@ -116,7 +117,12 @@ export class Linguini {
* @returns The retrieved language file item.
*/
public getRaw(location: string, langCode: string, variables?: { [name: string]: string }): any {
let data = JSON.parse(JSON.stringify(this.langDatas[langCode].data[location]));
let data = this.langDatas[langCode].data[location];
if (!data) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(`Language item '${location}' does not exist in '${filePath}'.`);
}
data = JSON.parse(JSON.stringify(this.langDatas[langCode].data[location]));
if (variables) {
data = DataUtils.replaceVariablesInObj(data, variables);
}
Expand All @@ -138,6 +144,12 @@ export class Linguini {
variables?: { [name: string]: string }
): string {
let ref = this.langDatas[langCode].refs[`REF:${location}`];
if (!ref) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(
`Reference string '${location}' does not exist in '${filePath}'.`
);
}
if (variables) {
ref = DataUtils.replaceVariablesInObj(ref, variables);
}
Expand All @@ -154,6 +166,12 @@ export class Linguini {
*/
public getCom(location: string, variables?: { [name: string]: string }): string {
let com = this.comData[`COM:${location}`];
if (!com) {
let filePath = path.join(this.folderPath, `${this.fileName}.common.json`);
throw new LinguiniError(
`Common reference string '${location}' does not exist in '${filePath}'.`
);
}
if (variables) {
com = DataUtils.replaceVariablesInObj(com, variables);
}
Expand Down
7 changes: 7 additions & 0 deletions src/models/error-models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class LinguiniError extends Error {
constructor(message?: string) {
super(message);
this.name = 'LinguiniError';
Object.setPrototypeOf(this, new.target.prototype);
}
}
42 changes: 40 additions & 2 deletions tests/unit/linguini.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { assert, expect } from 'chai';
import path from 'path';
import { Linguini, TypeMappers } from '../../src';
import { Linguini, LinguiniError, TypeMappers } from '../../src';

describe('Linguini', (): void => {
let linguini = new Linguini(path.join(__dirname, './data'), 'lang');
let folderPath = path.join(__dirname, './data');
let fileName = 'lang';
let linguini = new Linguini(folderPath, fileName);

describe('#get()', (): void => {
it('Regular expression', (): void => {
Expand Down Expand Up @@ -52,19 +54,55 @@ describe('Linguini', (): void => {
});
expect(greetingTwo).to.equal('Hello, nice to meet you Clark Kent!');
});

it('Does not exist', (): void => {
function myFunction(): void {
linguini.getRaw('badCategory.badName', 'en');
}

assert.throw(
myFunction,
LinguiniError,
`Language item 'badCategory.badName' does not exist in '${folderPath}\\lang.en.json'`
);
});
});

describe('#getRef()', (): void => {
it('Basic example', (): void => {
let ref = linguini.getRef('aboutMe.favoriteColor', 'en');
expect(ref).to.equal('Blue');
});

it('Does not exist', (): void => {
function myFunction(): void {
linguini.getRef('badCategory.badName', 'en');
}

assert.throw(
myFunction,
LinguiniError,
`Reference string 'badCategory.badName' does not exist in '${folderPath}\\lang.en.json'`
);
});
});

describe('#getCom()', (): void => {
it('Basic example', (): void => {
let com = linguini.getCom('links.github');
expect(com).to.equal('https://github.com/KevinNovak');
});

it('Does not exist', (): void => {
function myFunction(): void {
linguini.getCom('badCategory.badName');
}

assert.throw(
myFunction,
LinguiniError,
`Common reference string 'badCategory.badName' does not exist in '${folderPath}\\lang.common.json'`
);
});
});
});

0 comments on commit 275e591

Please sign in to comment.