diff options
| author | Leyla Jähnig <[email protected]> | 2022-03-31 22:42:37 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-03-31 22:42:37 +0200 |
| commit | c3be3b1945248ed422342b046ad765d252bbac05 (patch) | |
| tree | 0c4c6550092d675386937ff1ee8fd7f490c34711 /src | |
| parent | 48dcec18136e437dc537f0a58f30379a8ce531e8 (diff) | |
| download | faker-c3be3b1945248ed422342b046ad765d252bbac05.tar.xz faker-c3be3b1945248ed422342b046ad765d252bbac05.zip | |
feat: FakerError (#718)
Diffstat (limited to 'src')
| -rw-r--r-- | src/errors/faker-error.ts | 4 | ||||
| -rw-r--r-- | src/fake.ts | 7 | ||||
| -rw-r--r-- | src/faker.ts | 5 | ||||
| -rw-r--r-- | src/finance.ts | 3 | ||||
| -rw-r--r-- | src/index.ts | 1 | ||||
| -rw-r--r-- | src/mersenne.ts | 7 | ||||
| -rw-r--r-- | src/random.ts | 3 | ||||
| -rw-r--r-- | src/utils/unique.ts | 4 |
8 files changed, 24 insertions, 10 deletions
diff --git a/src/errors/faker-error.ts b/src/errors/faker-error.ts new file mode 100644 index 00000000..e38bdf47 --- /dev/null +++ b/src/errors/faker-error.ts @@ -0,0 +1,4 @@ +/** + * An error instance that will be thrown by faker. + */ +export class FakerError extends Error {} diff --git a/src/fake.ts b/src/fake.ts index b4775515..d478d01e 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import { FakerError } from './errors/faker-error'; /** * Generator method for combining faker methods based on string input. @@ -58,7 +59,7 @@ export class Fake { fake(str: string): string { // if incoming str parameter is not provided, return error message if (typeof str !== 'string' || str.length === 0) { - throw new Error('string parameter is required!'); + throw new FakerError('string parameter is required!'); } // find first matching {{ and }} @@ -88,11 +89,11 @@ export class Fake { const parts = method.split('.'); if (this.faker[parts[0]] == null) { - throw new Error('Invalid module: ' + parts[0]); + throw new FakerError('Invalid module: ' + parts[0]); } if (this.faker[parts[0]][parts[1]] == null) { - throw new Error('Invalid method: ' + parts[0] + '.' + parts[1]); + throw new FakerError('Invalid method: ' + parts[0] + '.' + parts[1]); } // assign the function from the module.function namespace diff --git a/src/faker.ts b/src/faker.ts index 1ace8501..de5c0c42 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -7,6 +7,7 @@ import { Datatype } from './datatype'; import { _Date } from './date'; import type { LocaleDefinition } from './definitions'; import { DEFINITIONS } from './definitions'; +import { FakerError } from './errors/faker-error'; import { Fake } from './fake'; import { Finance } from './finance'; import { Git } from './git'; @@ -83,13 +84,13 @@ export class Faker { constructor(opts: FakerOptions) { if (!opts) { - throw new Error( + throw new FakerError( 'Options with at least one entry in locales must be provided' ); } if (Object.keys(opts.locales ?? {}).length === 0) { - throw new Error( + throw new FakerError( 'At least one entry in locales must be provided in the locales parameter' ); } diff --git a/src/finance.ts b/src/finance.ts index 25d44046..ea71391d 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import { FakerError } from './errors/faker-error'; import type { Helpers } from './helpers'; import ibanLib from './iban'; @@ -329,7 +330,7 @@ export class Finance { } if (!ibanFormat) { - throw new Error('Country code ' + countryCode + ' not supported.'); + throw new FakerError('Country code ' + countryCode + ' not supported.'); } let s = ''; diff --git a/src/index.ts b/src/index.ts index d1c2cb1d..984833f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ export type { VehicleDefinitions, WordDefinitions, } from './definitions'; +export { FakerError } from './errors/faker-error'; export type { FakerOptions, UsableLocale, UsedLocales } from './faker'; export { Gender } from './name'; export type { GenderType } from './name'; diff --git a/src/mersenne.ts b/src/mersenne.ts index d9044e54..21f52f6c 100644 --- a/src/mersenne.ts +++ b/src/mersenne.ts @@ -1,3 +1,4 @@ +import { FakerError } from './errors/faker-error'; import Gen from './utils/mersenne'; /** @@ -46,7 +47,9 @@ export class Mersenne { */ seed(S: number): void { if (typeof S !== 'number') { - throw new Error('seed(S) must take numeric argument; is ' + typeof S); + throw new FakerError( + 'seed(S) must take numeric argument; is ' + typeof S + ); } this.gen.initGenrand(S); @@ -60,7 +63,7 @@ export class Mersenne { */ seed_array(A: number[]): void { if (typeof A !== 'object') { - throw new Error( + throw new FakerError( 'seed_array(A) must take array of numbers; is ' + typeof A ); } diff --git a/src/random.ts b/src/random.ts index f41a67fa..20d54f5b 100644 --- a/src/random.ts +++ b/src/random.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import { FakerError } from './errors/faker-error'; import { deprecated } from './internal/deprecated'; /** @@ -527,7 +528,7 @@ export class Random { } if (charsArray.length === 0) { - throw new Error( + throw new FakerError( 'Unable to generate string, because all possible characters are banned.' ); } diff --git a/src/utils/unique.ts b/src/utils/unique.ts index 78d0fdfc..2679b3d8 100644 --- a/src/utils/unique.ts +++ b/src/utils/unique.ts @@ -1,3 +1,5 @@ +import { FakerError } from '../errors/faker-error'; + export type RecordKey = string | number | symbol; // global results store @@ -41,7 +43,7 @@ function errorMessage( now - opts.startTime, 'ms' ); - throw new Error( + throw new FakerError( code + ' 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()' ); |
