@@ -3,22 +3,34 @@ import type { ClientPromiseResult } from './types'
33import { isDefinedError } from './error'
44
55export type SafeResult < TOutput , TError extends Error > =
6- | [ output : TOutput , error : undefined , isDefinedError : false ]
7- | [ output : undefined , error : TError , isDefinedError : false ]
8- | [ output : undefined , error : Extract < TError , ORPCError < any , any > > , isDefinedError : true ]
6+ | [ error : null , data : TOutput , isDefined : false ]
7+ & { error : null , data : TOutput , isDefined : false }
8+ | [ error : Exclude < TError , ORPCError < any , any > > , data : undefined , isDefined : false ]
9+ & { error : Exclude < TError , ORPCError < any , any > > , data : undefined , isDefined : false }
10+ | [ error : Extract < TError , ORPCError < any , any > > , data : undefined , isDefined : true ]
11+ & { error : Extract < TError , ORPCError < any , any > > , data : undefined , isDefined : true }
912
1013export async function safe < TOutput , TError extends Error > ( promise : ClientPromiseResult < TOutput , TError > ) : Promise < SafeResult < TOutput , TError > > {
1114 try {
1215 const output = await promise
13- return [ output , undefined , false ]
16+ return Object . assign (
17+ [ null , output , false ] satisfies [ null , TOutput , false ] ,
18+ { error : null , data : output , isDefined : false as const } ,
19+ )
1420 }
1521 catch ( e ) {
1622 const error = e as TError
1723
1824 if ( isDefinedError ( error ) ) {
19- return [ undefined , error , true ]
25+ return Object . assign (
26+ [ error , undefined , true ] satisfies [ typeof error , undefined , true ] ,
27+ { error, data : undefined , isDefined : true as const } ,
28+ )
2029 }
2130
22- return [ undefined , error , false ]
31+ return Object . assign (
32+ [ error as Exclude < TError , ORPCError < any , any > > , undefined , false ] satisfies [ Exclude < TError , ORPCError < any , any > > , undefined , false ] ,
33+ { error : error as Exclude < TError , ORPCError < any , any > > , data : undefined , isDefined : false as const } ,
34+ )
2335 }
2436}
0 commit comments