@brillout/libassert
微小的零依赖性工具,用于图书馆作者创建带有干净的跟踪痕迹的断言功能.
- 完整的堆栈痕迹。 (
Error.stackTraceLimit = Infinity;仅限 (a) 说明错误。 ) - 清理了堆放痕迹 (拳堆线指断言破解码,无用堆线被剥去
- 错误消息保证不包含新行 .
import { newError } from "@brillout/libassert";
export { assert };
function assert(condition: unknown): asserts condition {
if (condition) {
return;
}
const err = newError(
`[${libName}][Internal Error] Something unexpected happened, please open a GitHub issue.`;
);
throw err;
}
通话 newError(errorMessage) 与 new Error(errorMessage) 但:
- 如上所述,堆放痕迹是完整和清理的.
errorMessage禁止包含新行.
您可以创建各种断言功能, 例如 assertUsage 或者说 assertWarning数字 :
import { newError } from "@brillout/libassert";
export { assert, assertUsage, assertWarning };
const libName = "My Awesome Library";
// Assertions that are expected to always be true (also known as "invariants")
function assert(condition: unknown): asserts condition {
if (condition) {
return;
}
const err = newError(
`[${libName}][Internal Error] Something unexpected happened, please open a GitHub issue.`;
);
throw err;
}
// Wrong usage of your library
function assertUsage(
condition: unknown,
errorMessage: string
): asserts condition {
if (condition) {
return;
}
const err = newError(prefix: `[${libName}][Wrong Usage] ${errorMessage}`);
throw err;
}
// Something unexpected happened but it is non-critical and doesn't
// warrant interrupting code execution.
function assertWarning(condition: unknown, errorMessage: string): void {
if (condition) {
return;
}
const err = newError(`[${libName}][Warning] ${errorMessage}`);
console.warn(err);
}