deno.land / std@0.224.0 / assert / assert_is_error.ts

assert_is_error.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import { AssertionError } from "./assertion_error.ts";import { stripAnsiCode } from "../fmt/colors.ts";
/** * 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 * ```ts * import { assertIsError } from "https://deno.land/std@$STD_VERSION/assert/assert_is_error.ts"; * * 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 * ``` */export function assertIsError<E extends Error = Error>( error: unknown, // deno-lint-ignore no-explicit-any ErrorClass?: new (...args: any[]) => E, msgMatches?: string | RegExp, msg?: string,): asserts error is E { 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 "${ typeof error === "object" ? error?.constructor?.name : "[not an object]" }"${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 ${ error instanceof Error ? JSON.stringify(error.message) : '"[not an Error]"' // TODO(kt3k): show more useful information }${msgSuffix}`; throw new AssertionError(msg); }}
std

Version Info

Tagged at
3 weeks ago