Repository navigation
Description
Describe the Bug
When a Jobs task or workflow handler throws, Payload catches the error and throws a new TaskError / WorkflowError that copies only err.message. It does not set cause and does not keep err.stack.
V8 then captures a new stack at the wrapper throw, so logs, payload-jobs.error, and payload-jobs.log[].error all point at Payload internals instead of the handler.
Observed stack (handler threw Cannot read properties of undefined (reading 'id')):
Error: Cannot read properties of undefined (reading 'id')
at Object.handleAiConversationTask (.../payload/dist/queues/operations/runJobs/runJob/getRunTaskFunction.js:83:23)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
at async handler (.../payload/dist/queues/operations/runJobs/index.js:260:25)
...
The first frame is always getRunTaskFunction (throw new TaskError(...)). The application handler file never appears. That makes Jobs failures hard to debug, especially under Next.js where stacks are already compiled.
This is not a source-map problem. Source maps cannot recover frames that were never kept.
Root cause in packages/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts:
} catch (err: any) {
if (err instanceof JobCancelledError) {
throw err
}
throw new TaskError({
message: err.message || 'Task handler threw an error',
// no cause, no stack
})
}TaskError is super(args.message) only, so V8 captures a new stack at the wrapper. The persisted JSON in handleTaskError.ts / handleWorkflowError.ts stores error.stack, i.e. that wrapper stack.
The same wrap-without-cause happens in:
packages/payload/src/queues/operations/runJobs/runJob/index.ts(new WorkflowError({ message: error.message }))packages/payload/src/queues/operations/runJobs/runJSONJob/index.ts(same; JSON jobs also wrap aTaskErrorinWorkflowErrorwithoutinstanceof TaskErrorhandling)
APIError already uses super(message, { cause }).
Proposed fix: accept ErrorOptions on TaskError / WorkflowError, pass { cause: err }, and copy cause.stack onto the wrapper so job logs, pino (logger.error({ err })), and the admin UI keep showing the handler frames (they all read .stack, not cause).
export class TaskError extends Error {
args: TaskErrorArgs
constructor(args: TaskErrorArgs, options?: ErrorOptions) {
super(args.message, options)
this.name = 'TaskError'
this.args = args
if (options?.cause instanceof Error && typeof options.cause.stack === 'string') {
this.stack = options.cause.stack
}
}
}} catch (err: unknown) {
if (err instanceof JobCancelledError) throw err
throw new TaskError(
{
/* existing args */
message: err instanceof Error ? err.message : 'Task handler threw an error',
},
{ cause: err },
)
}Same { cause: error } at both new WorkflowError(...) call sites. Mapping those frames to TypeScript paths is a Next.js / --enable-source-maps concern and can stay out of this change.
Link to the code that reproduces this issue
Reproduction Steps
From the reproduction branch:
pnpm install
pnpm test:int job-wrong-backtraceThe suite in test/job-wrong-backtrace/ queues:
- A task whose handler throws from
throwFromTaskHandler.ts - A workflow whose handler throws from
throwFromWorkflowHandler.ts
It then asserts that the persisted job error.stack / log[].error.stack includes those handler names.
Actual: stack starts at getRunTaskFunction. The tests fail.
Expected: stack includes throwFromTaskHandler / throwFromWorkflowHandler (then the Jobs runner).
Minimal inline equivalent:
export const ThrowErrorTask: TaskConfig<'ThrowError'> = {
slug: 'ThrowError',
retries: 0,
handler: () => {
throw new Error('failed from my-task.ts')
},
}Queue and run the task, then inspect payload-jobs.log[].error.stack — it points at getRunTaskFunction, not my-task.ts.
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
Payload: 3.85.2 (also reproduces on current main)
Next.js: 16.2.6
Node.js: 24.18.0
Activity
Fnine59 commentedon Aug 31, 2026
Hi! I've opened PR #18061 addressing this issue.
Summary: fix: preserve job handler stack traces
The PR description includes the root-cause analysis and the RED/GREEN verification details.
fix: address six additional Payload v4 core and plugin issues [skip ci]