Skip to content

Commit

Permalink
feat(cdp): Additional logging of fetch errors (#24212)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Aug 6, 2024
1 parent 5e86434 commit 50c3a78
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
49 changes: 35 additions & 14 deletions plugin-server/src/cdp/hog-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,41 +168,62 @@ export class HogExecutor {
throw new Error('No hog function id provided')
}

const { logs = [], response = null, error: asyncError, timings = [] } = asyncFunctionResponse

if (response?.status && response.status >= 400) {
// Generic warn log for bad status codes
logs.push({
level: 'warn',
timestamp: DateTime.now(),
message: `Fetch returned bad status: ${response.status}`,
})
}

const errorRes = (error = 'Something went wrong'): HogFunctionInvocationResult => ({
invocation,
finished: false,
error,
logs: [],
logs: [
...logs,
{
level: 'error',
timestamp: DateTime.now(),
message: error,
},
],
})

const hogFunction = this.hogFunctionManager.getTeamHogFunction(
invocation.globals.project.id,
invocation.hogFunctionId
)

if (!hogFunction) {
return errorRes(`Hog Function with ID ${invocation.hogFunctionId} not found`)
}

if (!invocation.vmState || !asyncFunctionResponse.response || asyncFunctionResponse.error) {
return errorRes(asyncFunctionResponse.error ?? 'No VM state provided for async response')
if (!hogFunction || !invocation.vmState || asyncError) {
return errorRes(
!hogFunction
? `Hog Function with ID ${invocation.hogFunctionId} not found`
: asyncError
? asyncError
: 'No VM state provided for async response'
)
}

if (asyncFunctionResponse.response?.body && typeof asyncFunctionResponse.response?.body === 'string') {
// TODO: Ensure this is done in rusty hook
if (typeof response?.body === 'string') {
try {
asyncFunctionResponse.response.body = JSON.parse(asyncFunctionResponse.response.body)
} catch (e) {}
response.body = JSON.parse(response.body)
} catch (e) {
// pass - if it isn't json we just pass it on
}
}

// Add the response to the stack to continue execution
invocation.vmState.stack.push(convertJSToHog(asyncFunctionResponse.response ?? null))
invocation.timings.push(...(asyncFunctionResponse.timings ?? []))
invocation.vmState.stack.push(convertJSToHog(response))
invocation.timings.push(...timings)

const res = this.execute(hogFunction, invocation)

// Add any timings and logs from the async function
res.logs = [...(asyncFunctionResponse.logs ?? []), ...res.logs]
res.logs = [...(logs ?? []), ...res.logs]

return res
}
Expand Down
15 changes: 12 additions & 3 deletions plugin-server/src/cdp/hog-watcher/hog-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ export class HogWatcher {

if (currentState !== newState) {
transitionToState(id, newState)
// Extra logging to help debugging:

status.info('👀', `[HogWatcher] Function ${id} changed state`, {
oldState: currentState,
newState: newState,
ratings: newRatings,
})
}
})

Expand Down Expand Up @@ -390,9 +397,11 @@ export class HogWatcher {
return
}

status.info('👀', '[HogWatcher] Functions changed state', {
changes: stateChanges,
})
if (Object.keys(stateChanges.states).length) {
status.info('👀', '[HogWatcher] Functions changed state', {
changes: stateChanges,
})
}

// Finally write the state summary
const states: Record<HogFunctionType['id'], HogWatcherState> = Object.fromEntries(
Expand Down
5 changes: 4 additions & 1 deletion plugin-server/src/cdp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ export type HogFunctionAsyncFunctionResponse = {
/** An error message to indicate something went wrong and the invocation should be stopped */
error?: any
/** The data to be passed to the Hog function from the response */
response: any
response?: {
status: number
body: any
} | null
timings?: HogFunctionTiming[]
logs?: LogEntry[]
}
Expand Down

0 comments on commit 50c3a78

Please sign in to comment.