From 327f36af9492016c4cafba4c93d4e1a88b1cf03a Mon Sep 17 00:00:00 2001 From: David Mehren Date: Thu, 29 Apr 2021 16:06:59 +0200 Subject: [PATCH] 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 --- src/auth/auth.service.spec.ts | 6 ++++++ src/auth/auth.service.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index b79f7f0bf..ab13f1141 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -319,4 +319,10 @@ describe('AuthService', () => { ); }); }); + describe('randomString', () => { + it('throws on invalid lenght parameter', () => { + expect(() => service.randomString(0)).toThrow(); + expect(() => service.randomString(-1)).toThrow(); + }); + }); }); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 76758e680..45fa70280 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -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); }