Skip to content
This repository was archived by the owner on Apr 2, 2023. It is now read-only.

Commit 33a6115

Browse files
committed
fix: simpler PIITry
1 parent e39613d commit 33a6115

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

src/__tests__/try.spec.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import { PIITry, unwrap } from "../index"
2-
import { identity } from "../result"
32

43
describe("PIITry", () => {
54
it("should wrap results in PII", async () => {
65
const result = await PIITry(() => "TEST_STRING")
7-
const success = result.fold(identity, () => void 0)
8-
expect(unwrap(success)).toBe("TEST_STRING")
6+
expect(result).toBe("TEST_STRING")
97
})
108

119
it("should wrap exceptions in PII", async () => {
12-
const result = await PIITry<never, Error>(() => {
13-
throw new Error("Error message")
14-
})
15-
16-
const errorMessage = result.fold(
17-
() => "never",
18-
e => unwrap(e).message,
19-
)
20-
21-
expect(errorMessage).toBe("Error message")
10+
try {
11+
await PIITry(() => {
12+
throw new Error("Error message")
13+
})
14+
} catch (e) {
15+
expect(unwrap(e).message).toBe("Error message")
16+
}
2217
})
2318
})

src/result.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,10 @@ import { PII } from "./pii"
22

33
export const identity = <X>(x: X): X => x
44

5-
export class Ok<S, E> {
6-
constructor(private value: S) {}
7-
8-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9-
fold<R>(ok: (value: S) => R, _err: (err: E) => R): R {
10-
return ok(this.value)
11-
}
12-
}
13-
14-
export class Err<E, S> {
15-
constructor(private err: E) {}
16-
17-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18-
fold<R>(_ok: (value: S) => R, err: (err: E) => R): R {
19-
return err(this.err)
20-
}
21-
}
22-
23-
export type Result<S, E> = Ok<S, E> | Err<E, S>
24-
25-
export async function PIITry<S, E extends Error>(
26-
fn: () => S | Promise<S>,
27-
): Promise<Result<PII<S>, PII<E>>> {
5+
export async function PIITry<S>(fn: () => S | Promise<S>): Promise<S> {
286
try {
29-
return new Ok(PII(await fn()))
7+
return fn()
308
} catch (e) {
31-
return new Err(PII(e))
9+
throw PII(e)
3210
}
3311
}

0 commit comments

Comments
 (0)