2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2023-12-08 04:00:03 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
const {
|
|
|
|
Error,
|
|
|
|
} = primordials;
|
2023-06-27 02:18:22 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
/** 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");
|
|
|
|
}
|