Skip to content

Commit 4204582

Browse files
committed
feat: add invariant and tryCapture utils
1 parent 66ebe32 commit 4204582

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/core/util.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ export function extractCallerSource(): SourceType | undefined {
1212
if (!stack) return undefined
1313

1414
const lines = stack.split('\n')
15-
// Third line contains our called file and line
16-
// /Users/rasmus/Desktop/DEV/Web/flytrap-libs/test/newindex.test.ts:13:24
1715
if (lines.length < 4) return undefined
1816
const sourceLine = lines[3]
1917
const parts = sourceLine.split('/')
2018
const fileNameAndSourceLine = parts[parts.length - 1]
21-
// newindex.test.ts:13:24
2219
const [filePath, lineNumber] = fileNameAndSourceLine.split(':')
2320
return {
2421
filePath,
@@ -180,16 +177,13 @@ export function tryCatchSync<DType = unknown, EType = unknown>(
180177
}
181178
}
182179

183-
// function fillMissingFlytrapValues(lackingObject: any, fullObject: any) {}
184-
185180
type ReplaceValue = any
186181
type InputValue = ReplaceValue | (typeof FLYTRAP_REPLACE_VALUES)[number]
187182

188183
export function fillUnserializableFlytrapValues(
189184
input: InputValue,
190185
replaceValue: ReplaceValue
191186
): ReplaceValue {
192-
// if (input === FLYTRAP_UNSERIALIZABLE_VALUE) {
193187
if (FLYTRAP_REPLACE_VALUES.includes(input)) {
194188
if (replaceValue === undefined) throw new Error(`Replace value is undefined.`)
195189
return replaceValue

src/index.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
FlytrapFunctionOptions
77
} from './core/types'
88
import {
9+
TryCatchResponse,
910
fillUnserializableFlytrapValues,
1011
getMode,
1112
isAsyncFunction,
12-
tryCatch,
13-
tryCatchSync
13+
tryCatch as utilTryCatch,
14+
tryCatchSync as utilTryCatchSync
1415
} from './core/util'
1516
import { getFlytrapStorage, getLoadedCapture, loadAndPersist } from './core/storage'
1617
import { 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
494526
export * from './core/config'
495527
export * from './core/types'

0 commit comments

Comments
 (0)