Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(assert): add options parameter to AssertionError constructor #5561

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions assert/assertion_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@
* ```ts no-eval
* import { AssertionError } from "@std/assert";
*
* throw new AssertionError("Assertion failed");
* try {
* throw new AssertionError("foo", { cause: "bar" });
* } catch (error) {
* if (error instanceof AssertionError) {
* error.message === "foo"; // true
* error.cause === "bar"; // true
* }
* }
* ```
*/
export class AssertionError extends Error {
/** Constructs a new instance.
*
* @param message The error message.
* @param options Additional options.
*/
constructor(message: string) {
super(message);
constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = "AssertionError";
}
}
8 changes: 8 additions & 0 deletions assert/assertion_error_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, AssertionError, assertIsError } from "./mod.ts";

Deno.test("AssertionError", () => {
const error = new AssertionError("foo", { cause: { bar: "baz" } });
assertIsError(error, AssertionError, "foo");
assertEquals(error.cause, { bar: "baz" });
});
Loading