Skip to content

Commit ccfb509

Browse files
committed
feat(core): add codeOf utility and update exports and tests
Added `codeOf` utility to simplify retrieving error codes from `DefinedError`. Updated exports in `index.ts` to include all `of.ts` utilities and enhanced `neverthrow.test.ts` to validate error code retrieval.
1 parent 48f9343 commit ccfb509

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

packages/core/__tests__/neverthrow.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// noinspection ES6UnusedImports
22
import { describe, expect, test } from "bun:test";
3-
import { PayloadField, payloadOf, type ThatError } from "@thaterror/core";
3+
import { codeOf, PayloadField, payloadOf, type ThatError } from "@thaterror/core";
44
import { err, fromPromise } from 'neverthrow'
55
import { AppError } from './define-error.test'
66

@@ -40,6 +40,8 @@ describe('neverthrow test', () => {
4040
expect(result.error.is(AppError.NotFound)).toBe(true);
4141
const [id] = payloadOf(result.error);
4242
expect(id).toBe(404);
43+
const code = codeOf(result.error);
44+
expect(code).toBe("NotFound");
4345
}
4446
})
4547
})

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { That } from './define'
22
export { isDefinedError } from './is'
3-
export { payloadOf, scopeOf } from './of'
3+
export * from './of'
44

55
export * from './types'

packages/core/src/of.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type DefinedError, type ErrorCase, type ErrorFamily, PayloadField, ScopeField } from "./types";
1+
import { CodeField, type DefinedError, type ErrorCase, type ErrorFamily, PayloadField, ScopeField } from "./types";
22

33
export interface Scoped {
44
readonly [ScopeField]: symbol;
@@ -18,4 +18,8 @@ export function scopeOf(target: Scoped): symbol {
1818

1919
export function payloadOf<E extends DefinedError>(error: E): E[typeof PayloadField] {
2020
return error[PayloadField];
21+
}
22+
23+
export function codeOf<E extends DefinedError>(error: E): E[typeof CodeField] {
24+
return error[CodeField];
2125
}

0 commit comments

Comments
 (0)