Skip to content

Commit

Permalink
Merge pull request #38 from psychedelicious/psyche/feat/original-message
Browse files Browse the repository at this point in the history
feat(assert): store original message in `AssertionError`
  • Loading branch information
garronej authored Nov 9, 2024
2 parents 6a991e0 + 538da12 commit 04afa8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ export type { Equals } from "./Equals";

/** @see <https://docs.tsafe.dev/assert#error-thrown> */
export class AssertionError extends Error {
originalMessage?: string;

constructor(msg: string | undefined) {
super(`Wrong assertion encountered` + (!msg ? "" : `: "${msg}"`));

this.originalMessage = msg;

Object.setPrototypeOf(this, new.target.prototype);

if (!this.stack) {
Expand Down
9 changes: 9 additions & 0 deletions test/assertIs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,12 @@ scope: {
console.log("PASS");
}
}

/**
* `AssertionError` should store the original message in the `originalMessage` property.
*/
{
const error = new AssertionError("message");
assert(error.originalMessage === "message");
console.log("PASS");
}

0 comments on commit 04afa8b

Please sign in to comment.