diff options
Diffstat (limited to 'ext/node/polyfills/_util/asserts.ts')
-rw-r--r-- | ext/node/polyfills/_util/asserts.ts | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/ext/node/polyfills/_util/asserts.ts b/ext/node/polyfills/_util/asserts.ts new file mode 100644 index 000000000..7760c8639 --- /dev/null +++ b/ext/node/polyfills/_util/asserts.ts @@ -0,0 +1,21 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +/** Assertion error class for node compat layer's internal code. */ +export class NodeCompatAssertionError extends Error { + constructor(message: string) { + super(message); + this.name = "NodeCompatAssertionError"; + } +} + +/** Make an assertion, if not `true`, then throw. */ +export function assert(expr: unknown, msg = ""): asserts expr { + if (!expr) { + throw new NodeCompatAssertionError(msg); + } +} + +/** Use this to assert unreachable code. */ +export function unreachable(): never { + throw new NodeCompatAssertionError("unreachable"); +} |