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' ;
55import type { IsNever , RequiredKeysOf } from 'type-fest' ;
66
77import { objectify } from './object.js' ;
@@ -30,8 +30,12 @@ type ExceptionConstructorArgs<TParams extends ExceptionParams, TOptions extends
3030 : [ message : string , options : TOptions ] ;
3131
3232type 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