MCPcopy Index your code
hub / github.com/denoland/std / assertIsError

Function assertIsError

assert/is_error.ts:29–65  ·  view source on GitHub ↗
(
  error: unknown,
  // deno-lint-ignore no-explicit-any
  ErrorClass?: abstract new (...args: any[]) => E,
  msgMatches?: string | RegExp,
  msg?: string,
)

Source from the content-addressed store, hash-verified

27 * @param msg The optional message to display if the assertion fails.
28 */
29export function assertIsError<E extends Error = Error>(
30 error: unknown,
31 // deno-lint-ignore no-explicit-any
32 ErrorClass?: abstract new (...args: any[]) => E,
33 msgMatches?: string | RegExp,
34 msg?: string,
35): asserts error is E {
36 const msgSuffix = msg ? `: ${msg}` : ".";
37 if (!(error instanceof Error)) {
38 throw new AssertionError(
39 `Expected "error" to be an Error object${msgSuffix}`,
40 );
41 }
42 if (ErrorClass && !(error instanceof ErrorClass)) {
43 msg =
44 `Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
45 throw new AssertionError(msg);
46 }
47 let msgCheck;
48 if (typeof msgMatches === "string") {
49 msgCheck = stripAnsiCode(error.message).includes(
50 stripAnsiCode(msgMatches),
51 );
52 }
53 if (msgMatches instanceof RegExp) {
54 msgCheck = msgMatches.test(stripAnsiCode(error.message));
55 }
56
57 if (msgMatches && !msgCheck) {
58 msg = `Expected error message to include ${
59 msgMatches instanceof RegExp
60 ? msgMatches.toString()
61 : JSON.stringify(msgMatches)
62 }, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
63 throw new AssertionError(msg);
64 }
65}

Callers 7

is_error_test.tsFile · 0.90
assertRejectsFunction · 0.90
assertThrowsFunction · 0.90
assertSpyCallsFunction · 0.50
assertSpyCallFunction · 0.50
assertSpyCallAsyncFunction · 0.50

Calls 4

stripAnsiCodeFunction · 0.90
includesMethod · 0.80
stringifyMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…