Skip to content

Commit

Permalink
Merge branch 'main' into 736-address-nearbygpscoordinate-non-determin…
Browse files Browse the repository at this point in the history
…istic-results
  • Loading branch information
ST-DDT authored Mar 31, 2022
2 parents 63dfc0b + c3be3b1 commit d7a9317
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/errors/faker-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* An error instance that will be thrown by faker.
*/
export class FakerError extends Error {}
7 changes: 4 additions & 3 deletions src/fake.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import { FakerError } from './errors/faker-error';

/**
* Generator method for combining faker methods based on string input.
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/finance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import { FakerError } from './errors/faker-error';
import type { Helpers } from './helpers';
import ibanLib from './iban';

Expand Down Expand Up @@ -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 = '';
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 5 additions & 2 deletions src/mersenne.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FakerError } from './errors/faker-error';
import Gen from './utils/mersenne';

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/random.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import { FakerError } from './errors/faker-error';
import { deprecated } from './internal/deprecated';

/**
Expand Down Expand Up @@ -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.'
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/unique.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FakerError } from '../errors/faker-error';

export type RecordKey = string | number | symbol;

// global results store
Expand Down Expand Up @@ -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()'
);
Expand Down
7 changes: 4 additions & 3 deletions test/fake.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { FakerError } from '../src/errors/faker-error';

describe('fake', () => {
describe('fake()', () => {
Expand Down Expand Up @@ -32,18 +33,18 @@ describe('fake', () => {
expect(() =>
// @ts-expect-error: The parameter is required
faker.fake()
).toThrowError(Error('string parameter is required!'));
).toThrowError(new FakerError('string parameter is required!'));
});

it('does not allow invalid module name', () => {
expect(() => faker.fake('{{foo.bar}}')).toThrowError(
Error('Invalid module: foo')
new FakerError('Invalid module: foo')
);
});

it('does not allow invalid method name', () => {
expect(() => faker.fake('{{address.foo}}')).toThrowError(
Error('Invalid method: address.foo')
new FakerError('Invalid method: address.foo')
);
});

Expand Down
7 changes: 5 additions & 2 deletions test/faker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { faker, Faker } from '../src';
import { FakerError } from '../src/errors/faker-error';

describe('faker', () => {
beforeEach(() => {
Expand All @@ -12,7 +13,9 @@ describe('faker', () => {
// @ts-expect-error: mission options
new Faker()
).toThrow(
Error('Options with at least one entry in locales must be provided')
new FakerError(
'Options with at least one entry in locales must be provided'
)
);
});

Expand All @@ -22,7 +25,7 @@ describe('faker', () => {
// @ts-expect-error: missing locales
new Faker({})
).toThrow(
Error(
new FakerError(
'At least one entry in locales must be provided in the locales parameter'
)
);
Expand Down
5 changes: 4 additions & 1 deletion test/finance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { FakerError } from '../src/errors/faker-error';
import ibanLib from '../src/iban';
import { luhnCheck } from './support/luhnCheck';

Expand Down Expand Up @@ -490,7 +491,9 @@ describe('finance', () => {
expect(() =>
faker.finance.iban(false, unsupportedCountryCode)
).toThrowError(
Error(`Country code ${unsupportedCountryCode} not supported.`)
new FakerError(
`Country code ${unsupportedCountryCode} not supported.`
)
)
);
});
Expand Down
7 changes: 5 additions & 2 deletions test/mersenne.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { FakerError } from '../src/errors/faker-error';
import { Mersenne } from '../src/mersenne';

type SeededRun = {
Expand Down Expand Up @@ -181,7 +182,9 @@ describe('mersenne twister', () => {
// @ts-expect-error: non-integer error
'abc'
)
).toThrowError(Error('seed(S) must take numeric argument; is string'));
).toThrowError(
new FakerError('seed(S) must take numeric argument; is string')
);
});

it('should throw an error when attempting to seed() a non-integer', () => {
Expand All @@ -191,7 +194,7 @@ describe('mersenne twister', () => {
'abc'
)
).toThrowError(
Error('seed_array(A) must take array of numbers; is string')
new FakerError('seed_array(A) must take array of numbers; is string')
);
});
});
11 changes: 11 additions & 0 deletions test/unique.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { FakerError } from '../src/errors/faker-error';

const seededRuns = [
{
Expand Down Expand Up @@ -120,6 +121,16 @@ describe('unique', () => {
});
}).toThrowError(/^Exceeded maxRetries:/);
});

it('should throw a FakerError instance on error', () => {
expect(() => {
faker.unique(faker.internet.protocol, [], {
maxTime: 5000,
maxRetries: 5,
exclude: ['https', 'http'],
});
}).toThrowError(FakerError);
});
});
}
});
Expand Down

0 comments on commit d7a9317

Please sign in to comment.