From c7a8ba44cb531881f8aa72e49726ff6928576b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Fri, 12 Jul 2019 21:18:18 +0200 Subject: [PATCH 1/3] Fix exhaustiveness of error code switch in http status conversion --- src/providers/https.ts | 3 ++- src/utilities/assertions.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/utilities/assertions.ts diff --git a/src/providers/https.ts b/src/providers/https.ts index e6c8a63f4..3c3999123 100644 --- a/src/providers/https.ts +++ b/src/providers/https.ts @@ -27,6 +27,7 @@ import * as _ from 'lodash'; import { apps } from '../apps'; import { HttpsFunction, optionsToTrigger, Runnable } from '../cloud-functions'; import { DeploymentOptions } from '../function-configuration'; +import { assertNever } from '../utilities/assertions'; export interface Request extends express.Request { rawBody: Buffer; @@ -231,7 +232,7 @@ export class HttpsError extends Error { return 500; // This should never happen as long as the type system is doing its job. default: - throw new Error('Invalid error code: ' + this.code); + assertNever(this.code); } } diff --git a/src/utilities/assertions.ts b/src/utilities/assertions.ts new file mode 100644 index 000000000..b02434733 --- /dev/null +++ b/src/utilities/assertions.ts @@ -0,0 +1,13 @@ +/** + * @file Provides common assertion helpers which can be used to improve + * strictness of both type checking and runtime. + */ + + /** + * Checks that the given value is of type `never` — the type that’s left after + * all other cases have been removed. + * @param x A value of type `never`. + */ +export function assertNever(x: never): never { + throw new Error(`Unhandled discriminated union member: ${JSON.stringify(x)}`); +} From c476b09cd064bfda7020567be9aec0f826b299e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Fri, 12 Jul 2019 21:18:59 +0200 Subject: [PATCH 2/3] Add a dot at the end of an error message --- src/utilities/assertions.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/utilities/assertions.ts b/src/utilities/assertions.ts index b02434733..525a9d5b3 100644 --- a/src/utilities/assertions.ts +++ b/src/utilities/assertions.ts @@ -3,11 +3,13 @@ * strictness of both type checking and runtime. */ - /** - * Checks that the given value is of type `never` — the type that’s left after - * all other cases have been removed. - * @param x A value of type `never`. - */ +/** + * Checks that the given value is of type `never` — the type that’s left after + * all other cases have been removed. + * @param x A value of type `never`. + */ export function assertNever(x: never): never { - throw new Error(`Unhandled discriminated union member: ${JSON.stringify(x)}`); + throw new Error( + `Unhandled discriminated union member: ${JSON.stringify(x)}.` + ); } From 634a01a23055923c07daa738c70d7e1a2dda76d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Fri, 12 Jul 2019 21:26:00 +0200 Subject: [PATCH 3/3] Add a newline before parameter documentation --- src/utilities/assertions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utilities/assertions.ts b/src/utilities/assertions.ts index 525a9d5b3..49ff5e36d 100644 --- a/src/utilities/assertions.ts +++ b/src/utilities/assertions.ts @@ -6,6 +6,7 @@ /** * Checks that the given value is of type `never` — the type that’s left after * all other cases have been removed. + * * @param x A value of type `never`. */ export function assertNever(x: never): never {