@@ -6,11 +6,12 @@ import {
66 FlytrapFunctionOptions
77} from './core/types'
88import {
9+ TryCatchResponse ,
910 fillUnserializableFlytrapValues ,
1011 getMode ,
1112 isAsyncFunction ,
12- tryCatch ,
13- tryCatchSync
13+ tryCatch as utilTryCatch ,
14+ tryCatchSync as utilTryCatchSync
1415} from './core/util'
1516import { getFlytrapStorage , getLoadedCapture , loadAndPersist } from './core/storage'
1617import { createHumanLog } from './core/human-logs'
@@ -68,7 +69,7 @@ export function useFlytrapFunction<
6869 */
6970 saveErrorForFunction ( opts . id , error )
7071 log . info ( 'capture' , `Captured error in async function with ID "${ opts . id } ".` , { error } )
71- const { error : saveError } = await tryCatch (
72+ const { error : saveError } = await utilTryCatch (
7273 getFlytrapStorage ( ) . saveCapture ( _executingFunctions , _functionCalls , error as Error )
7374 )
7475 if ( saveError ) {
@@ -131,7 +132,7 @@ export function useFlytrapFunction<
131132 */
132133 saveErrorForFunction ( opts . id , error )
133134 log . info ( 'capture' , `Captured error in function with ID "${ opts . id } ".` , { error } )
134- const { error : saveError } = tryCatchSync ( ( ) =>
135+ const { error : saveError } = utilTryCatchSync ( ( ) =>
135136 getFlytrapStorage ( ) . saveCapture ( _executingFunctions , _functionCalls , error as Error )
136137 )
137138 if ( saveError ) {
@@ -373,7 +374,7 @@ export async function capture<T extends Error>({
373374 }
374375
375376 // Let's save it
376- const { error : saveError } = await tryCatch (
377+ const { error : saveError } = await utilTryCatch (
377378 getFlytrapStorage ( ) . saveCapture ( _executingFunctions , _functionCalls , error )
378379 )
379380 if ( saveError ) {
@@ -490,6 +491,37 @@ function saveErrorForFunction(functionId: string, error: any) {
490491 if ( lastInvocation ) lastInvocation . error = serializeError ( error )
491492}
492493
494+ // Utility functions
495+ export async function tryCapture < DType = unknown , EType = any > (
496+ fn : Promise < DType >
497+ ) : Promise < TryCatchResponse < DType , EType > > {
498+ try {
499+ return { data : await fn , error : null }
500+ } catch ( error ) {
501+ await capture ( { error : error as Error } )
502+ return { data : null , error : error as EType }
503+ }
504+ }
505+ export function tryCaptureSync < DType = unknown , EType = any > (
506+ fn : ( ) => DType
507+ ) : TryCatchResponse < DType , EType > {
508+ try {
509+ return { data : fn ( ) , error : null }
510+ } catch ( error ) {
511+ capture ( { error : error as Error } )
512+ return { data : null , error : error as EType }
513+ }
514+ }
515+
516+ export async function invariantAsync ( condition : any , message ?: string ) {
517+ if ( condition ) return
518+ await capture ( { error : new Error ( 'Invariant failed' ) , message : message ?? 'Invariant failed.' } )
519+ }
520+ export function invariant ( condition : any , message ?: string ) : asserts condition {
521+ if ( condition ) return
522+ capture ( { error : new Error ( 'Invariant failed' ) , message : message ?? 'Invariant failed.' } )
523+ }
524+
493525// Export
494526export * from './core/config'
495527export * from './core/types'
0 commit comments