2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
2024-05-15 05:00:04 +00:00
|
|
|
import { stripAnsiCode } from "@std/internal/styles";
|
2023-07-13 07:04:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2023-12-06 17:13:38 +00:00
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertIsError } from "@std/assert";
|
2023-12-06 17:13:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertIsError<E extends Error = Error>(
|
|
|
|
error: unknown,
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-09-17 06:28:22 +00:00
|
|
|
ErrorClass?: abstract new (...args: any[]) => E,
|
2023-12-12 14:45:58 +00:00
|
|
|
msgMatches?: string | RegExp,
|
2023-07-13 07:04:30 +00:00
|
|
|
msg?: string,
|
|
|
|
): asserts error is E {
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
2023-12-12 14:45:58 +00:00
|
|
|
if (!(error instanceof Error)) {
|
2023-07-13 07:04:30 +00:00
|
|
|
throw new AssertionError(
|
2024-10-25 11:04:05 +00:00
|
|
|
`Expected "error" to be an Error object${msgSuffix}`,
|
2023-07-13 07:04:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (ErrorClass && !(error instanceof ErrorClass)) {
|
2024-05-07 00:08:16 +00:00
|
|
|
msg =
|
|
|
|
`Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
|
2023-07-13 07:04:30 +00:00
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
2023-12-12 14:45:58 +00:00
|
|
|
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) {
|
2023-08-29 11:23:07 +00:00
|
|
|
msg = `Expected error message to include ${
|
2023-12-12 14:45:58 +00:00
|
|
|
msgMatches instanceof RegExp
|
|
|
|
? msgMatches.toString()
|
|
|
|
: JSON.stringify(msgMatches)
|
2024-05-07 00:08:16 +00:00
|
|
|
}, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
|
2023-07-13 07:04:30 +00:00
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|