Skip to content

Commit c3be3b1

Browse files
feat: FakerError (#718)
1 parent 48dcec1 commit c3be3b1

13 files changed

Lines changed: 53 additions & 18 deletions

File tree

src/errors/faker-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* An error instance that will be thrown by faker.
3+
*/
4+
export class FakerError extends Error {}

src/fake.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '.';
2+
import { FakerError } from './errors/faker-error';
23

34
/**
45
* Generator method for combining faker methods based on string input.
@@ -58,7 +59,7 @@ export class Fake {
5859
fake(str: string): string {
5960
// if incoming str parameter is not provided, return error message
6061
if (typeof str !== 'string' || str.length === 0) {
61-
throw new Error('string parameter is required!');
62+
throw new FakerError('string parameter is required!');
6263
}
6364

6465
// find first matching {{ and }}
@@ -88,11 +89,11 @@ export class Fake {
8889
const parts = method.split('.');
8990

9091
if (this.faker[parts[0]] == null) {
91-
throw new Error('Invalid module: ' + parts[0]);
92+
throw new FakerError('Invalid module: ' + parts[0]);
9293
}
9394

9495
if (this.faker[parts[0]][parts[1]] == null) {
95-
throw new Error('Invalid method: ' + parts[0] + '.' + parts[1]);
96+
throw new FakerError('Invalid method: ' + parts[0] + '.' + parts[1]);
9697
}
9798

9899
// assign the function from the module.function namespace

src/faker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Datatype } from './datatype';
77
import { _Date } from './date';
88
import type { LocaleDefinition } from './definitions';
99
import { DEFINITIONS } from './definitions';
10+
import { FakerError } from './errors/faker-error';
1011
import { Fake } from './fake';
1112
import { Finance } from './finance';
1213
import { Git } from './git';
@@ -83,13 +84,13 @@ export class Faker {
8384

8485
constructor(opts: FakerOptions) {
8586
if (!opts) {
86-
throw new Error(
87+
throw new FakerError(
8788
'Options with at least one entry in locales must be provided'
8889
);
8990
}
9091

9192
if (Object.keys(opts.locales ?? {}).length === 0) {
92-
throw new Error(
93+
throw new FakerError(
9394
'At least one entry in locales must be provided in the locales parameter'
9495
);
9596
}

src/finance.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '.';
2+
import { FakerError } from './errors/faker-error';
23
import type { Helpers } from './helpers';
34
import ibanLib from './iban';
45

@@ -329,7 +330,7 @@ export class Finance {
329330
}
330331

331332
if (!ibanFormat) {
332-
throw new Error('Country code ' + countryCode + ' not supported.');
333+
throw new FakerError('Country code ' + countryCode + ' not supported.');
333334
}
334335

335336
let s = '';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type {
2626
VehicleDefinitions,
2727
WordDefinitions,
2828
} from './definitions';
29+
export { FakerError } from './errors/faker-error';
2930
export type { FakerOptions, UsableLocale, UsedLocales } from './faker';
3031
export { Gender } from './name';
3132
export type { GenderType } from './name';

src/mersenne.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FakerError } from './errors/faker-error';
12
import Gen from './utils/mersenne';
23

34
/**
@@ -46,7 +47,9 @@ export class Mersenne {
4647
*/
4748
seed(S: number): void {
4849
if (typeof S !== 'number') {
49-
throw new Error('seed(S) must take numeric argument; is ' + typeof S);
50+
throw new FakerError(
51+
'seed(S) must take numeric argument; is ' + typeof S
52+
);
5053
}
5154

5255
this.gen.initGenrand(S);
@@ -60,7 +63,7 @@ export class Mersenne {
6063
*/
6164
seed_array(A: number[]): void {
6265
if (typeof A !== 'object') {
63-
throw new Error(
66+
throw new FakerError(
6467
'seed_array(A) must take array of numbers; is ' + typeof A
6568
);
6669
}

src/random.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '.';
2+
import { FakerError } from './errors/faker-error';
23
import { deprecated } from './internal/deprecated';
34

45
/**
@@ -527,7 +528,7 @@ export class Random {
527528
}
528529

529530
if (charsArray.length === 0) {
530-
throw new Error(
531+
throw new FakerError(
531532
'Unable to generate string, because all possible characters are banned.'
532533
);
533534
}

src/utils/unique.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { FakerError } from '../errors/faker-error';
2+
13
export type RecordKey = string | number | symbol;
24

35
// global results store
@@ -41,7 +43,7 @@ function errorMessage(
4143
now - opts.startTime,
4244
'ms'
4345
);
44-
throw new Error(
46+
throw new FakerError(
4547
code +
4648
' for uniqueness check \n\nMay not be able to generate any more unique values with current settings. \nTry adjusting maxTime or maxRetries parameters for faker.unique()'
4749
);

test/fake.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { faker } from '../src';
3+
import { FakerError } from '../src/errors/faker-error';
34

45
describe('fake', () => {
56
describe('fake()', () => {
@@ -32,18 +33,18 @@ describe('fake', () => {
3233
expect(() =>
3334
// @ts-expect-error: The parameter is required
3435
faker.fake()
35-
).toThrowError(Error('string parameter is required!'));
36+
).toThrowError(new FakerError('string parameter is required!'));
3637
});
3738

3839
it('does not allow invalid module name', () => {
3940
expect(() => faker.fake('{{foo.bar}}')).toThrowError(
40-
Error('Invalid module: foo')
41+
new FakerError('Invalid module: foo')
4142
);
4243
});
4344

4445
it('does not allow invalid method name', () => {
4546
expect(() => faker.fake('{{address.foo}}')).toThrowError(
46-
Error('Invalid method: address.foo')
47+
new FakerError('Invalid method: address.foo')
4748
);
4849
});
4950

test/faker.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { beforeEach, describe, expect, it } from 'vitest';
22
import { faker, Faker } from '../src';
3+
import { FakerError } from '../src/errors/faker-error';
34

45
describe('faker', () => {
56
beforeEach(() => {
@@ -12,7 +13,9 @@ describe('faker', () => {
1213
// @ts-expect-error: mission options
1314
new Faker()
1415
).toThrow(
15-
Error('Options with at least one entry in locales must be provided')
16+
new FakerError(
17+
'Options with at least one entry in locales must be provided'
18+
)
1619
);
1720
});
1821

@@ -22,7 +25,7 @@ describe('faker', () => {
2225
// @ts-expect-error: missing locales
2326
new Faker({})
2427
).toThrow(
25-
Error(
28+
new FakerError(
2629
'At least one entry in locales must be provided in the locales parameter'
2730
)
2831
);

0 commit comments

Comments
 (0)