This repository was archived by the owner on Aug 31, 2023. It is now read-only.
generated from esdmr/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make every function accept formatted message (#9)
* Accept message formatting in WrappedError * Fix error properties being enumerable * Allow formatting messages in every function
- Loading branch information
Showing
15 changed files
with
346 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@esdmr/assert": minor | ||
--- | ||
|
||
New function `wrap` to format messages before creating a `WrappedError`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@esdmr/assert": minor | ||
--- | ||
|
||
Allow assertions to be `detail`ed with contextual information. Additionally, this | ||
allows the full message to be formatted. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@esdmr/assert": patch | ||
--- | ||
|
||
Fix `PrimitiveError` and `WrappedError` having enumerable properties, duplicating output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,37 @@ | ||
import { AssertionError } from './errors.js'; | ||
import { AssertionError, WrappedError } from './errors.js'; | ||
import { DEFAULT_MESSAGE } from './messages.js'; | ||
import { format } from './utils.js'; | ||
|
||
/** | ||
* Formats strings for `assert` function. | ||
* Asserts that a given condition is true. | ||
* | ||
* @public | ||
* @param condition - The given condition. | ||
* @param message - The message to include in the error. Formatted with `{}`. | ||
* @param args - Format arguments. | ||
* @returns The formatted string. | ||
*/ | ||
function format (message: string, ...args: unknown[]) { | ||
for (const item of args) { | ||
message = message.replace('{}', String(item)); | ||
export function assert ( | ||
condition: boolean, | ||
message = DEFAULT_MESSAGE, | ||
...args: unknown[] | ||
): asserts condition { | ||
if (!condition) { | ||
throw new AssertionError(format(message, ...args)); | ||
} | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* Asserts that a given condition is true. It formats the message provided with | ||
* the arguments after that which are stringified via `String`. | ||
* Wraps any thrown value. | ||
* | ||
* @public | ||
* @param condition - The given condition. | ||
* @param thrownValue - The value to wrap. | ||
* @param message - The message to include in the error. Formatted with `{}`. | ||
* @param args - Format arguments. | ||
*/ | ||
export function assert ( | ||
condition: boolean, | ||
export function wrap ( | ||
thrownValue: unknown, | ||
message = DEFAULT_MESSAGE, | ||
...args: unknown[] | ||
): asserts condition { | ||
if (condition) { | ||
return; | ||
} | ||
|
||
throw new AssertionError(format(message, ...args)); | ||
) { | ||
return new WrappedError(format(message, ...args), thrownValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.