UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

74 lines (73 loc) 3.21 kB
import "@js-temporal/polyfill"; import "urlpattern-polyfill"; globalThis.addEventListener = () => {}; import { l as AssertionError, o as stripAnsiCode } from "./assert_equals-Ew3jOFa3.mjs"; //#region ../../node_modules/.pnpm/@jsr+std__assert@0.226.0/node_modules/@jsr/std__assert/assert_is_error.js /** * Make an assertion that `error` is an `Error`. * If not then an error will be thrown. * An error class and a string that should be included in the * error message can also be asserted. * * @example Usage * ```ts no-eval * import { assertIsError } from "@std/assert/assert-is-error"; * * assertIsError(null); // Throws * assertIsError(new RangeError("Out of range")); // Doesn't throw * assertIsError(new RangeError("Out of range"), SyntaxError); // Throws * assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw * assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws * ``` * * @typeParam E The type of the error to assert. * @param error The error to assert. * @param ErrorClass The optional error class to assert. * @param msgMatches The optional string or RegExp to assert in the error message. * @param msg The optional message to display if the assertion fails. */ function assertIsError(error, ErrorClass, msgMatches, msg) { const msgSuffix = msg ? `: ${msg}` : "."; if (!(error instanceof Error)) throw new AssertionError(`Expected "error" to be an Error object${msgSuffix}}`); if (ErrorClass && !(error instanceof ErrorClass)) { msg = `Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`; throw new AssertionError(msg); } let msgCheck; if (typeof msgMatches === "string") msgCheck = stripAnsiCode(error.message).includes(stripAnsiCode(msgMatches)); if (msgMatches instanceof RegExp) msgCheck = msgMatches.test(stripAnsiCode(error.message)); if (msgMatches && !msgCheck) { msg = `Expected error message to include ${msgMatches instanceof RegExp ? msgMatches.toString() : JSON.stringify(msgMatches)}, but got ${JSON.stringify(error?.message)}${msgSuffix}`; throw new AssertionError(msg); } } //#endregion //#region ../../node_modules/.pnpm/@jsr+std__assert@0.226.0/node_modules/@jsr/std__assert/assert_throws.js function assertThrows(fn, errorClassOrMsg, msgIncludesOrMsg, msg) { let ErrorClass = void 0; let msgIncludes = void 0; let err; if (typeof errorClassOrMsg !== "string") if (errorClassOrMsg === void 0 || errorClassOrMsg?.prototype instanceof Error || errorClassOrMsg?.prototype === Error.prototype) { ErrorClass = errorClassOrMsg; msgIncludes = msgIncludesOrMsg; } else msg = msgIncludesOrMsg; else msg = errorClassOrMsg; let doesThrow = false; const msgSuffix = msg ? `: ${msg}` : "."; try { fn(); } catch (error) { if (ErrorClass) { if (error instanceof Error === false) throw new AssertionError(`A non-Error object was thrown${msgSuffix}`); assertIsError(error, ErrorClass, msgIncludes, msg); } err = error; doesThrow = true; } if (!doesThrow) { msg = `Expected function to throw${msgSuffix}`; throw new AssertionError(msg); } return err; } //#endregion export { assertIsError as n, assertThrows as t };