Skip to content

Commit 760efeb

Browse files
committed
fix(core): mark the correct function as errored when capturing manually
1 parent 95d591e commit 760efeb

4 files changed

Lines changed: 47 additions & 10 deletions

File tree

examples/with-express/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import nodeResolve from '@rollup/plugin-node-resolve'
33
import commonjs from '@rollup/plugin-commonjs'
44
import { FlytrapTransformPlugin } from 'useflytrap/transform'
55

6+
/** @type import('rollup').RollupOptions */
67
export default {
78
input: 'src/index.ts',
89
output: {

src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function useFlytrapFunction<
3232
args,
3333
timestamp: Date.now()
3434
})
35+
_executionCursor--
3536

3637
// Replaying code
3738
if (getMode() === 'replay') {
@@ -49,17 +50,16 @@ export function useFlytrapFunction<
4950

5051
// Merge replay args & real args
5152
const mergedArgs = fillUnserializableFlytrapValues(replayArgs, args)
52-
// const realOutput = await fn(...mergedArgs)
5353
// @ts-expect-error for some reason `this as any` still gives error
5454
const realOutput = await executeFunctionAsync(fn, this as any, mergedArgs)
5555
return fillUnserializableFlytrapValues(replayOutput, realOutput)
5656
}
5757
// Capturing bugs
5858
try {
59-
// const output = await fn(...args)
6059
// @ts-expect-error for some reason `this as any` still gives error
6160
const output = await executeFunctionAsync(fn, this as any, args)
6261
saveOutputForFunction(opts.id, output)
62+
_executionCursor++
6363
return output
6464
} catch (error) {
6565
/**
@@ -94,6 +94,7 @@ export function useFlytrapFunction<
9494
args,
9595
timestamp: Date.now()
9696
})
97+
_executionCursor--
9798

9899
// Replaying code
99100
if (getMode() === 'replay') {
@@ -105,14 +106,12 @@ export function useFlytrapFunction<
105106
`Could not find replay input arguments for function with ID ${opts.id}, either this is a function you have added, or something has gone wrong during the capture process. If this is the case, contact us.`
106107
)
107108
}
108-
// return fn(...args)
109109
// @ts-expect-error for some reason `this as any` still gives error
110110
return executeFunction(fn, this as any, args)
111111
}
112112

113113
// Merge replay args & real args
114114
const mergedArgs = fillUnserializableFlytrapValues(replayArgs, args)
115-
// const realOutput = fn(...mergedArgs)
116115
// @ts-expect-error for some reason `this as any` still gives error
117116
const realOutput = executeFunction(fn, this as any, mergedArgs)
118117
return fillUnserializableFlytrapValues(replayOutput, realOutput)
@@ -123,6 +122,7 @@ export function useFlytrapFunction<
123122
// @ts-expect-error for some reason `this as any` still gives error
124123
const output = executeFunction(fn, this as any, args)
125124
saveOutputForFunction(opts.id, output)
125+
_executionCursor++
126126
return output
127127
} catch (error) {
128128
/**
@@ -324,12 +324,17 @@ function getReplayFunctionOutput(functionId: string): any | undefined {
324324
}
325325

326326
let _executingFunctions: CapturedFunction[] = []
327+
let _executionCursor = 1
327328
let _functionCalls: CapturedCall[] = []
328329
let _userId: string | undefined = undefined
329330

330331
export const getCapturedFunctions = () => _executingFunctions
331332
export const getCapturedCalls = () => _functionCalls
332333

334+
export const getUserId = () => _userId
335+
export const getExecutingFunction = () =>
336+
_executingFunctions.at(-_executionCursor) ?? _executingFunctions.at(-1)
337+
333338
export const clearCapturedFunctions = () => {
334339
_executingFunctions = []
335340
}
@@ -392,8 +397,6 @@ export function identify(userId: string) {
392397
log.info('identify', `Identified current user as "${userId}"`)
393398
_userId = userId
394399
}
395-
export const getUserId = () => _userId
396-
export const getExecutingFunction = () => _executingFunctions.at(-1)
397400

398401
const addFunctionInvocation = (functionId: string, invocation: CaptureInvocation) => {
399402
log.info('function-execution', `Executing function with ID "${functionId}"`)

src/transform/artifacts/cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ export async function upsertArtifacts(
8989
const uploadedBatches = await batchedArtifactsUpload(artifactsToUpload, secretApiKey, projectId)
9090
log.info(
9191
'storage',
92-
`Pushed ${
93-
artifactsToUpload.length
94-
} artifacts in ${uploadedBatches?.length} batches to the Flytrap API. Payload Size: ${formatBytes(
92+
`Pushed ${artifactsToUpload.length} artifacts in ${
93+
uploadedBatches?.length
94+
} batches to the Flytrap API. Payload Size: ${formatBytes(
9595
JSON.stringify(artifactsToUpload).length
9696
)}`
9797
)

test/capture.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
useFlytrapCall,
44
clearCapturedFunctions,
55
clearCapturedCalls,
6-
getCapturedCalls
6+
getCapturedCalls,
7+
getExecutingFunction,
8+
getCapturedFunctions
79
} from '../src/index'
810
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
911

@@ -90,6 +92,37 @@ describe('useFlytrapCall capture', () => {
9092
expect(returnVal).toBe(2)
9193
})
9294

95+
it('correctly marks function causing the error when manually capturing', () => {
96+
const date = new Date(0)
97+
vi.setSystemTime(date)
98+
clearFlytrapStorage()
99+
100+
let caughtExecutingFunctionId: string | undefined = undefined
101+
102+
const GET = useFlytrapFunction(
103+
function GET() {
104+
const users = [1, 2, 3, 4, 5]
105+
const foundUser = useFlytrapCall(users, {
106+
id: '/file.js-call-_find',
107+
args: [
108+
useFlytrapFunction((u) => u === 13, {
109+
id: '/file.js-_anonymous'
110+
})
111+
],
112+
name: 'find'
113+
})
114+
if (!foundUser) {
115+
// `capture` here
116+
caughtExecutingFunctionId = getExecutingFunction()?.id
117+
}
118+
},
119+
{ id: '/file.js-_GET' }
120+
)
121+
GET()
122+
123+
expect(caughtExecutingFunctionId).toEqual(`/file.js-_GET`)
124+
})
125+
93126
it.todo('captures errors inside the function call')
94127
})
95128

0 commit comments

Comments
 (0)