Skip to content

Commit 36916b8

Browse files
merlinnotthechenky
authored andcommitted
Fix exhaustiveness of error code switch in http status conversion (#520)
* Fix exhaustiveness of error code switch in http status conversion * Add a dot at the end of an error message * Add a newline before parameter documentation
1 parent f7df0c7 commit 36916b8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/providers/https.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as _ from 'lodash';
2727
import { apps } from '../apps';
2828
import { HttpsFunction, optionsToTrigger, Runnable } from '../cloud-functions';
2929
import { DeploymentOptions } from '../function-configuration';
30+
import { assertNever } from '../utilities/assertions';
3031

3132
export interface Request extends express.Request {
3233
rawBody: Buffer;
@@ -231,7 +232,7 @@ export class HttpsError extends Error {
231232
return 500;
232233
// This should never happen as long as the type system is doing its job.
233234
default:
234-
throw new Error('Invalid error code: ' + this.code);
235+
assertNever(this.code);
235236
}
236237
}
237238

src/utilities/assertions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @file Provides common assertion helpers which can be used to improve
3+
* strictness of both type checking and runtime.
4+
*/
5+
6+
/**
7+
* Checks that the given value is of type `never` — the type that’s left after
8+
* all other cases have been removed.
9+
*
10+
* @param x A value of type `never`.
11+
*/
12+
export function assertNever(x: never): never {
13+
throw new Error(
14+
`Unhandled discriminated union member: ${JSON.stringify(x)}.`
15+
);
16+
}

0 commit comments

Comments
 (0)