Skip to content

Commit

Permalink
Merge pull request #4 from lambdalisue/prefer-jsr
Browse files Browse the repository at this point in the history
Prefer jsr
  • Loading branch information
lambdalisue authored May 14, 2024
2 parents 910df8a + 8dcdd30 commit a0733d2
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 108 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# errorutil

[![JSR](https://jsr.io/badges/@lambdalisue/errorutil)](https://jsr.io/@lambdalisue/errorutil)
[![denoland](https://img.shields.io/github/v/release/lambdalisue/deno-errorutil?logo=deno&label=denoland)](https://deno.land/x/errorutil)
[![npm](http://img.shields.io/badge/available%20on-npm-lightgrey.svg?logo=npm&logoColor=white)](https://www.npmjs.com/package/@lambdalisue/errorutil)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/errorutil/mod.ts)
[![Test](https://github.com/lambdalisue/deno-errorutil/workflows/Test/badge.svg)](https://github.com/lambdalisue/deno-errorutil/actions?query=workflow%3ATest)
[![npm version](https://badge.fury.io/js/@lambdalisue%2Ferrorutil.svg)](https://badge.fury.io/js/@lambdalisue%2Ferrorutil)
[![codecov](https://codecov.io/gh/lambdalisue/deno-errorutil/graph/badge.svg?token=O0cA5Xj5la)](https://codecov.io/gh/lambdalisue/deno-errorutil)

A utility pack for handling error.

[deno]: https://deno.land/

> [!WARN]
>
> The package on [deno.land] and [npm] is deprecated. Use the package on
> [jsr.io] instead.
>
> ```
> $ deno add @lambdalisue/errorutil
> $ npx jsr add @lambdalisue/errorutil
> ```
[deno.land]: https://deno.land/x/errorutil
[npm]: https://www.npmjs.com/package/@lambdalisue/errorutil
[jsr.io]: https://jsr.io/@lambdalisue/errorutil
## License
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
Expand Down
18 changes: 16 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@lambdalisue/errorutil",
"version": "0.0.0",
"exports": "./mod.ts",
"exports": {
".": "./mod.ts",
"./error-object": "./error_object.ts",
"./raise": "./raise.ts",
"./try-or": "./try_or.ts",
"./try-or-else": "./try_or_else.ts",
"./unimplemented": "./unimplemented.ts",
"./unreachable": "./unreachable.ts"
},
"exclude": [
".coverage",
"npm"
Expand All @@ -16,6 +24,12 @@
"update:commit": "deno task -q update --commit --pre-commit=fmt,lint"
},
"imports": {
"https://deno.land/x/errorutil@$MODULE_VERSION/": "./"
"@lambdalisue/errorutil": "./mod.ts",
"@lambdalisue/errorutil/error-object": "./error_object.ts",
"@lambdalisue/errorutil/raise": "./raise.ts",
"@lambdalisue/errorutil/try-or": "./try_or.ts",
"@lambdalisue/errorutil/try-or-else": "./try_or_else.ts",
"@lambdalisue/errorutil/unimplemented": "./unimplemented.ts",
"@lambdalisue/errorutil/unreachable": "./unreachable.ts"
}
}
2 changes: 1 addition & 1 deletion error_object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is } from "jsr:@core/unknownutil";
import { is } from "jsr:@core/unknownutil@3.18.0";

export type ErrorObject = {
proto: string;
Expand Down
2 changes: 1 addition & 1 deletion error_object_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertInstanceOf } from "jsr:@std/assert";
import { assertEquals, assertInstanceOf } from "jsr:@std/assert@0.225.1";
import { fromErrorObject, toErrorObject } from "./error_object.ts";

class CustomError extends Error {
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./error_object.ts";
export * from "./raise.ts";
export * from "./try_or.ts";
export * from "./try_or_else.ts";
export * from "./unimplemented.ts";
export * from "./unreachable.ts";
2 changes: 1 addition & 1 deletion raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This is function thus can be used as an expression.
*
* ```typescript
* import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts";
* import { raise } from "@lambdalisue/errorutil/raise";
*
* const fn = () => raise(new Error("fail"));
* ```
Expand Down
2 changes: 1 addition & 1 deletion raise_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertThrows } from "jsr:@std/assert";
import { assertThrows } from "jsr:@std/assert@0.225.1";
import { raise } from "./raise.ts";

Deno.test("raise", () => {
Expand Down
41 changes: 3 additions & 38 deletions try_or.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { tryOrElse } from "./try_or_else.ts";
/**
* Try to execute a function and return the result or a default value.
*
* ```ts
* import { tryOr } from "https://deno.land/x/errorutil@$MODULE_VERSION/try_or.ts";
* import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts";
* import { tryOr } from "@lambdalisue/errorutil/try-or";
* import { raise } from "@lambdalisue/errorutil/raise";
*
* // Sync
* console.log(tryOr(() => 1, 2)); // 1
Expand All @@ -25,39 +26,3 @@ export function tryOr<T>(
): T | Promise<T> {
return tryOrElse(fn, () => orValue);
}

/**
* Try to execute a function and return the result or execute another function.
*
* ```ts
* import { tryOrElse } from "https://deno.land/x/errorutil@$MODULE_VERSION/try_or.ts";
* import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts";
*
* // Sync
* console.log(tryOrElse(() => 1, () => 2)); // 1
* console.log(tryOrElse(() => raise("err"), () => 2)); // 2
*
* // Async
* console.log(await tryOrElse(() => Promise.resolve(1), () => 2)); // 1
* console.log(await tryOrElse(() => Promise.reject("err"), () => 2)); // 2
* ```
*/
export function tryOrElse<T>(fn: () => T, elseFn: (err: unknown) => T): T;
export function tryOrElse<T>(
fn: () => Promise<T>,
elseFn: (err: unknown) => T | Promise<T>,
): Promise<T>;
export function tryOrElse<T>(
fn: () => T | Promise<T>,
elseFn: (err: unknown) => T | Promise<T>,
): T | Promise<T> {
try {
const ret = fn();
if (ret instanceof Promise) {
return ret.catch((err) => elseFn(err));
}
return ret;
} catch (err) {
return elseFn(err);
}
}
35 changes: 35 additions & 0 deletions try_or_else.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Try to execute a function and return the result or execute another function.
*
* ```ts
* import { tryOrElse } from "@lambdalisue/errorutil/try-or-else";
* import { raise } from "@lambdalisue/errorutil/raise";
*
* // Sync
* console.log(tryOrElse(() => 1, () => 2)); // 1
* console.log(tryOrElse(() => raise("err"), () => 2)); // 2
*
* // Async
* console.log(await tryOrElse(() => Promise.resolve(1), () => 2)); // 1
* console.log(await tryOrElse(() => Promise.reject("err"), () => 2)); // 2
* ```
*/
export function tryOrElse<T>(fn: () => T, elseFn: (err: unknown) => T): T;
export function tryOrElse<T>(
fn: () => Promise<T>,
elseFn: (err: unknown) => T | Promise<T>,
): Promise<T>;
export function tryOrElse<T>(
fn: () => T | Promise<T>,
elseFn: (err: unknown) => T | Promise<T>,
): T | Promise<T> {
try {
const ret = fn();
if (ret instanceof Promise) {
return ret.catch((err) => elseFn(err));
}
return ret;
} catch (err) {
return elseFn(err);
}
}
62 changes: 62 additions & 0 deletions try_or_else_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
assertEquals,
assertRejects,
assertThrows,
} from "jsr:@std/[email protected]";
import { raise } from "./raise.ts";
import { tryOrElse } from "./try_or_else.ts";

Deno.test("tryOrElse", async (t) => {
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);

await t.step("sync", () => {
type T = number;
assertEquals(tryOrElse((): T => 1, () => 2), 1);
assertEquals(tryOrElse((): T => raise(err), () => 2), 2);
});

await t.step("sync (error)", () => {
type T = number;
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});

await t.step("async", async () => {
type T = Promise<number>;
assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1);
assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1);
assertEquals(await tryOrElse((): T => reject(err), () => 2), 2);
assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2);
assertEquals(await tryOrElse((): T => raise(err), () => 2), 2);
assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2);
});

await t.step("async (error)", async () => {
type T = Promise<number>;
await assertRejects(
() => tryOrElse((): T => reject(err), () => reject(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => reject(err), () => raise(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => raise(err), () => reject(err)),
Error,
"error",
);
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});
});
59 changes: 2 additions & 57 deletions try_or_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals, assertRejects, assertThrows } from "jsr:@std/assert";
import { assertEquals } from "jsr:@std/assert@0.225.1";
import { raise } from "./raise.ts";
import { tryOr, tryOrElse } from "./try_or.ts";
import { tryOr } from "./try_or.ts";

Deno.test("tryOr", async (t) => {
const err = new Error("error");
Expand All @@ -23,58 +23,3 @@ Deno.test("tryOr", async (t) => {
assertEquals(await tryOr((): T => raise(err), resolve(2)), 2);
});
});

Deno.test("tryOrElse", async (t) => {
const err = new Error("error");
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);

await t.step("sync", () => {
type T = number;
assertEquals(tryOrElse((): T => 1, () => 2), 1);
assertEquals(tryOrElse((): T => raise(err), () => 2), 2);
});

await t.step("sync (error)", () => {
type T = number;
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});

await t.step("async", async () => {
type T = Promise<number>;
assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1);
assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1);
assertEquals(await tryOrElse((): T => reject(err), () => 2), 2);
assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2);
assertEquals(await tryOrElse((): T => raise(err), () => 2), 2);
assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2);
});

await t.step("async (error)", async () => {
type T = Promise<number>;
await assertRejects(
() => tryOrElse((): T => reject(err), () => reject(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => reject(err), () => raise(err)),
Error,
"error",
);
await assertRejects(
() => tryOrElse((): T => raise(err), () => reject(err)),
Error,
"error",
);
assertThrows(
() => tryOrElse((): T => raise(err), () => raise(err)),
Error,
"error",
);
});
});
2 changes: 1 addition & 1 deletion unimplemented.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UnimplementedError extends Error {
* For example, defining a mock object with `unimplemented` function should look like this:
*
* ```ts
* import { unimplemented } from "https://deno.land/x/errorutil@$MODULE_VERSION/unimplemented.ts";
* import { unimplemented } from "@lambdalisue/errorutil/unimplemented";
*
* type Service = {
* get(id: string): Promise<string>;
Expand Down
4 changes: 2 additions & 2 deletions unreachable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UnreachableError extends Error {
* For example, the following code passed type checking.
*
* ```ts
* import { unreachable } from "https://deno.land/x/errorutil@$MODULE_VERSION/unreachable.ts";
* import { unreachable } from "@lambdalisue/errorutil/unreachable";
*
* type Animal = "dog" | "cat";
*
Expand All @@ -41,7 +41,7 @@ export class UnreachableError extends Error {
* But the following code because a case for "bird" is missing.
*
* ```ts
* import { unreachable } from "https://deno.land/x/errorutil@$MODULE_VERSION/unreachable.ts";
* import { unreachable } from "@lambdalisue/errorutil/unreachable";
*
* type Animal = "dog" | "cat" | "bird";
*
Expand Down

0 comments on commit a0733d2

Please sign in to comment.