Skip to content

Commit f130740

Browse files
committed
feat: add new toAsyncErr method on exceptions
1 parent f70fcb6 commit f130740

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/__tests__/exception.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Err } from 'neverthrow';
12
import type { Simplify } from 'type-fest';
23
import { describe, expect, expectTypeOf, it, test } from 'vitest';
34

@@ -119,6 +120,20 @@ describe('ValueException', () => {
119120
it('should have the correct prototype', () => {
120121
expect(Object.getPrototypeOf(ValueException)).toBe(BaseException);
121122
});
123+
it('should have the asErr static method', () => {
124+
expect(ValueException.asErr()).toBeInstanceOf(Err);
125+
});
126+
it('should have the asAsyncErr static method', async () => {
127+
expect(await ValueException.asAsyncErr()).toBeInstanceOf(Err);
128+
});
129+
it('should have the toErr method', () => {
130+
const exception = new ValueException();
131+
expect(exception.toErr()).toBeInstanceOf(Err);
132+
});
133+
it('should have the asAsyncErr static method', async () => {
134+
const exception = new ValueException();
135+
expect(await exception.toAsyncErr()).toBeInstanceOf(Err);
136+
});
122137
});
123138

124139
describe('OutOfRangeException', () => {

src/exception.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-return */
22
/* eslint-disable no-dupe-class-members */
33

4-
import { Err, err } from 'neverthrow';
4+
import { err, errAsync, Result, ResultAsync } from 'neverthrow';
55
import type { IsNever, RequiredKeysOf } from 'type-fest';
66

77
import { objectify } from './object.js';
@@ -30,8 +30,12 @@ type ExceptionConstructorArgs<TParams extends ExceptionParams, TOptions extends
3030
: [message: string, options: TOptions];
3131

3232
type ExceptionStatic<TParams extends ExceptionParams, TOptions extends ExceptionOptions> = {
33-
/** return an instance of the exception wrapped in a neverthrow Error object */
34-
asErr(...args: ExceptionConstructorArgs<TParams, TOptions>): Err<never, BaseException<TParams, TOptions>>;
33+
/** return an instance of the exception wrapped in a neverthrow Result */
34+
asAsyncErr(
35+
...args: ExceptionConstructorArgs<TParams, TOptions>
36+
): ResultAsync<never, BaseException<TParams, TOptions>>;
37+
/** return an instance of the exception wrapped in a neverthrow Result */
38+
asErr(...args: ExceptionConstructorArgs<TParams, TOptions>): Result<never, BaseException<TParams, TOptions>>;
3539
/** inference-only property that will be undefined at runtime */
3640
Instance: BaseException<TParams, TOptions>;
3741
};
@@ -57,7 +61,11 @@ abstract class BaseException<TParams extends ExceptionParams, TOptions extends E
5761
this.details = options?.details;
5862
}
5963

60-
toErr(): Err<never, this> {
64+
toAsyncErr(): ResultAsync<never, this> {
65+
return errAsync(this);
66+
}
67+
68+
toErr(): Result<never, this> {
6169
return err(this);
6270
}
6371
}
@@ -95,9 +103,14 @@ class ExceptionBuilder<
95103
}
96104
super(message, options);
97105
}
106+
static asAsyncErr(
107+
...args: ExceptionConstructorArgs<NonNullable<TParams>, TOptions>
108+
): ResultAsync<never, BaseException<NonNullable<TParams>, TOptions>> {
109+
return new this(...args).toAsyncErr();
110+
}
98111
static asErr(
99112
...args: ExceptionConstructorArgs<NonNullable<TParams>, TOptions>
100-
): Err<never, BaseException<NonNullable<TParams>, TOptions>> {
113+
): Result<never, BaseException<NonNullable<TParams>, TOptions>> {
101114
return new this(...args).toErr();
102115
}
103116
};

0 commit comments

Comments
 (0)