AuthService.randomString: Throw Error instead of returning null

A string with a negative length is invalid, so we should throw here
instead of complicating the type with a possible null return value.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-04-29 16:06:59 +02:00
parent 5ee9b2a7e8
commit 327f36af94
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 7 additions and 1 deletions

View file

@ -319,4 +319,10 @@ describe('AuthService', () => {
);
});
});
describe('randomString', () => {
it('throws on invalid lenght parameter', () => {
expect(() => service.randomString(0)).toThrow();
expect(() => service.randomString(-1)).toThrow();
});
});
});

View file

@ -64,7 +64,7 @@ export class AuthService {
randomString(length: number): Buffer {
if (length <= 0) {
return null;
throw new Error('randomString cannot have a length < 1');
}
return randomBytes(length);
}