summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_util/asserts.ts
blob: 7760c8639738da40e22cc19b8e8c2d9983ed68bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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");
}