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: add alter and alterElse to alter thrown error #4

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@

A utility pack for handling error.

## alter / alterElse

`alter` and `alterElse` are functions that execute a function and return the
result. If the function throws an error, `alter` throws the given error, and
`alterElse` throws the result of the second function.

```ts
import { assertThrows } from "@std/assert";
import { alter } from "@core/errorutil/alter";
import { alterElse } from "@core/errorutil/alter-else";

const fn = () => {
throw new Error("This is an error message");
};

assertThrows(() => alter(fn, new Error("custom error")), Error, "custom error");

assertThrows(
() => alterElse(fn, () => new Error("custom error")),
Error,
"custom error",
);
```

## attempt

`attempt` is a function that executes a function and returns the result
Expand Down
23 changes: 23 additions & 0 deletions alter.ts
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;
}
}
23 changes: 23 additions & 0 deletions alter_else.ts
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);
}
}
16 changes: 16 additions & 0 deletions alter_else_test.ts
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",
);
});
16 changes: 16 additions & 0 deletions alter_test.ts
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",
);
});
4 changes: 4 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.0.0",
"exports": {
".": "./mod.ts",
"./alter": "./alter.ts",
"./alter-else": "./alter_else.ts",
"./attempt": "./attempt.ts",
"./error-object": "./error_object.ts",
"./raise": "./raise.ts",
Expand All @@ -28,6 +30,8 @@
},
"imports": {
"@core/errorutil": "./mod.ts",
"@core/errorutil/alter": "./alter.ts",
"@core/errorutil/alter-else": "./alter_else.ts",
"@core/errorutil/attempt": "./attempt.ts",
"@core/errorutil/error-object": "./error_object.ts",
"@core/errorutil/raise": "./raise.ts",
Expand Down