-
-
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.
Merge pull request #4 from jsr-core/add-alter
feat: add `alter` and `alterElse` to alter thrown error
- Loading branch information
Showing
6 changed files
with
106 additions
and
0 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
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,23 @@ | ||
/** | ||
* Alter the error of a function if an error is thrown. | ||
* | ||
* @param fn - The function to execute. | ||
* @param alt - The value to throw if an error is thrown. | ||
* @returns The result of the function. | ||
* @throws The value of alt. | ||
* @example | ||
* | ||
* ```ts | ||
* import { alter } from "@core/errorutil/alter"; | ||
* | ||
* console.log(alter(() => 1, "err2")); // 1 | ||
* console.log(alter(() => { throw "err1" }, "err2")); // "err2" is thrown | ||
* ``` | ||
*/ | ||
export function alter<T, E>(fn: () => T, alt: E): T { | ||
try { | ||
return fn(); | ||
} catch { | ||
throw alt; | ||
} | ||
} |
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,23 @@ | ||
/** | ||
* Alter the error of a function if an error is thrown. | ||
* | ||
* @param fn - The function to execute. | ||
* @param modifier - The function to execute if an error is thrown. | ||
* @returns The result of the function. | ||
* @throws The result of the modifier function. | ||
* @example | ||
* | ||
* ```ts | ||
* import { alterElse } from "@core/errorutil/alter-else"; | ||
* | ||
* console.log(alterElse(() => 1, () => "err")); // 1 | ||
* console.log(alterElse(() => { throw "err" }, (err) => "new " + err)); // "new err" is thrown | ||
* ``` | ||
*/ | ||
export function alterElse<T, E>(fn: () => T, elseFn: (err: unknown) => E): T { | ||
try { | ||
return fn(); | ||
} catch (err) { | ||
throw elseFn(err); | ||
} | ||
} |
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,16 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { raise } from "./raise.ts"; | ||
import { alterElse } from "./alter_else.ts"; | ||
|
||
test("alterElse should return a function result", () => { | ||
assertEquals(alterElse(() => 1, () => "err2"), 1); | ||
}); | ||
|
||
test("alterElse should throws an alt when the function throws error", () => { | ||
assertThrows( | ||
() => alterElse(() => raise("err"), (err) => new Error(`new ${err}`)), | ||
Error, | ||
"new err", | ||
); | ||
}); |
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,16 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { raise } from "./raise.ts"; | ||
import { alter } from "./alter.ts"; | ||
|
||
test("alter should return a function result", () => { | ||
assertEquals(alter(() => 1, "err2"), 1); | ||
}); | ||
|
||
test("alter should throws an alt when the function throws error", () => { | ||
assertThrows( | ||
() => alter(() => raise("err1"), new Error("err2")), | ||
Error, | ||
"err2", | ||
); | ||
}); |
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