-
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.
- Loading branch information
Showing
4 changed files
with
179 additions
and
9 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,99 @@ | ||
import {test, expect, vi} from "vitest" | ||
import {ResultGroup, createGroup} from "./group" | ||
import {ResultError} from ".." | ||
|
||
test("constructor", () => { | ||
const handleError = vi.fn() | ||
const result = createGroup(handleError) | ||
expect(result).toBeInstanceOf(ResultGroup) | ||
// @ts-expect-error | ||
expect(result.handleError).toBe(handleError) | ||
}) | ||
|
||
class FooError extends ResultError { | ||
readonly tag = "foo" | ||
} | ||
|
||
class BarError extends ResultError { | ||
readonly tag = "bar" | ||
} | ||
|
||
const m = createGroup(() => new FooError()) | ||
|
||
test("tryFn", () => { | ||
const result = m.tryFn(() => { | ||
throw new Error("error") | ||
}) | ||
expect(result.unwrapErr()).toBeInstanceOf(FooError) | ||
}) | ||
|
||
test("tryFnWith", () => { | ||
const result = m.tryFnWith( | ||
() => { | ||
throw new Error("error") | ||
}, | ||
() => new BarError(), | ||
) | ||
expect(result.unwrapErr()).toBeInstanceOf(BarError) | ||
}) | ||
|
||
test("tryPromise", async () => { | ||
const result = m.tryPromise(Promise.reject(new Error("error"))) | ||
await expect(result.unwrapErr()).resolves.toBeInstanceOf(FooError) | ||
}) | ||
|
||
test("tryPromiseWith", async () => { | ||
const result = m.tryPromiseWith(Promise.reject(new Error("error")), () => new BarError()) | ||
await expect(result.unwrapErr()).resolves.toBeInstanceOf(BarError) | ||
}) | ||
|
||
test("tryAsyncFn", async () => { | ||
const result = m.tryAsyncFn(async () => { | ||
throw new Error("error") | ||
}) | ||
await expect(result.unwrapErr()).resolves.toBeInstanceOf(FooError) | ||
}) | ||
|
||
test("tryAsyncFnWith", async () => { | ||
const result = m.tryAsyncFnWith( | ||
async () => { | ||
throw new Error("error") | ||
}, | ||
() => new BarError(), | ||
) | ||
await expect(result.unwrapErr()).resolves.toBeInstanceOf(BarError) | ||
}) | ||
|
||
test("guard", () => { | ||
const f = m.guard(() => { | ||
throw new Error("error") | ||
}) | ||
expect(f().unwrapErr()).toBeInstanceOf(FooError) | ||
}) | ||
|
||
test("guardWith", () => { | ||
const f = m.guardWith( | ||
() => { | ||
throw new Error("error") | ||
}, | ||
() => new BarError(), | ||
) | ||
expect(f().unwrapErr()).toBeInstanceOf(BarError) | ||
}) | ||
|
||
test("guardAsync", async () => { | ||
const f = m.guardAsync(async () => { | ||
throw new Error("error") | ||
}) | ||
await expect(f().unwrapErr()).resolves.toBeInstanceOf(FooError) | ||
}) | ||
|
||
test("guardAsyncWith", async () => { | ||
const f = m.guardAsyncWith( | ||
async () => { | ||
throw new Error("error") | ||
}, | ||
() => new BarError(), | ||
) | ||
await expect(f().unwrapErr()).resolves.toBeInstanceOf(BarError) | ||
}) |
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,73 @@ | ||
import {tryAsyncFnWith, tryFnWith, tryPromiseWith} from "./try" | ||
import type {ErrorHandler, ResultError} from "../error/result_error" | ||
import type {Result} from "../result/interface" | ||
import type {PromiseResult} from "../result/promise" | ||
|
||
export type ResultGroupErrorHandler<E extends ResultError, F extends ResultError> = (error: E) => F | ||
|
||
type Fn = (...args: any[]) => any | ||
type AsyncFn = (...args: any[]) => Promise<any> | ||
|
||
export class ResultGroup<E extends ResultError> { | ||
private constructor(private readonly handleError: ErrorHandler<E>) {} | ||
|
||
tryFn<T>(f: () => T): Result<T, E> { | ||
return tryFnWith(f, this.handleError) | ||
} | ||
|
||
tryFnWith<T, F extends ResultError>( | ||
f: () => T, | ||
h: ResultGroupErrorHandler<E, F>, | ||
): Result<T, F> { | ||
return tryFnWith(f, (error) => h(this.handleError(error))) | ||
} | ||
|
||
tryPromise<T>(promise: Promise<T>): PromiseResult<T, E> { | ||
return tryPromiseWith(promise, this.handleError) | ||
} | ||
|
||
tryPromiseWith<T, F extends ResultError>( | ||
promise: Promise<T>, | ||
h: ResultGroupErrorHandler<E, F>, | ||
): PromiseResult<T, F> { | ||
return tryPromiseWith(promise, (error) => h(this.handleError(error))) | ||
} | ||
|
||
tryAsyncFn<T>(f: () => Promise<T>): PromiseResult<T, E> { | ||
return tryAsyncFnWith(f, this.handleError) | ||
} | ||
|
||
tryAsyncFnWith<T, F extends ResultError>( | ||
f: () => Promise<T>, | ||
h: ResultGroupErrorHandler<E, F>, | ||
): PromiseResult<T, F> { | ||
return tryAsyncFnWith(f, (error) => h(this.handleError(error))) | ||
} | ||
|
||
guard<T extends Fn>(f: T) { | ||
return (...args: Parameters<T>) => this.tryFn(() => f(...args)) | ||
} | ||
|
||
guardWith<T extends Fn, F extends ResultError>(f: T, h: ResultGroupErrorHandler<E, F>) { | ||
return (...args: Parameters<T>) => this.tryFnWith(() => f(...args), h) | ||
} | ||
|
||
guardAsync<T extends AsyncFn>(f: T) { | ||
return (...args: Parameters<T>) => this.tryAsyncFn(() => f(...args)) | ||
} | ||
|
||
guardAsyncWith<T extends AsyncFn, F extends ResultError>( | ||
f: T, | ||
h: ResultGroupErrorHandler<E, F>, | ||
) { | ||
return (...args: Parameters<T>) => this.tryAsyncFnWith(() => f(...args), h) | ||
} | ||
|
||
static with<E extends ResultError>(handleError: ErrorHandler<E>): ResultGroup<E> { | ||
return new ResultGroup(handleError) | ||
} | ||
} | ||
|
||
export function createGroup<E extends ResultError>(handleError: ErrorHandler<E>): ResultGroup<E> { | ||
return ResultGroup.with(handleError) | ||
} |
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