Skip to content

Commit

Permalink
Merge pull request #354 from camunda/fix-351+
Browse files Browse the repository at this point in the history
Fix 351+

fixes #351
  • Loading branch information
jwulf authored Jan 23, 2025
2 parents 4498144 + 448575c commit fa368b5
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/zeebe/zb/ZBWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ export class ZBWorker<
`Caught an unhandled exception in a task handler for process instance ${job.processInstanceKey}:`
)
this.logger.logDebug(job)
this.logger.logError((e as Error).message)
// If the exception has a details field, log it
// This is the case for exceptions thrown when the job is not found. The details field contains an explanation.
const hasDetails = (e: unknown): e is { details?: string } =>
!!(e as { details: string }).details
if (hasDetails(e)) {
this.logger.logError(e.details)
} else {
this.logger.logError((e as Error).message)
}
if (this.cancelWorkflowOnException) {
const { processInstanceKey } = job
this.logger.logDebug(
Expand All @@ -74,15 +82,36 @@ export class ZBWorker<
this.drainOne()
}
} else {
this.logger.logInfo(`Failing job ${job.key}`)
const message = (e as Error).message
// This is *most probably* an error thrown because the job was not found when job.complete() or job.fail() was called.
// It could also happen in some cases where the handler does another operation that returns an error with the same code.
if (
message.includes('5 NOT_FOUND') &&
message.includes(job.key) &&
(message.includes('COMPLETE') || message.includes('FAIL'))
) {
this.logger.logDebug(
`Job ${job.key} was already completed or failed, or the process instance was cancelled. Ignoring.`
)
this.drainOne()
return
}
this.logger.logInfo(`Failing job ${job.key} due to unhandled exception`)
const retries = job.retries - 1
try {
this.zbClient.failJob({
errorMessage: `Unhandled exception in task handler ${e}`,
jobKey: job.key,
retries,
retryBackOff: 0,
})
this.zbClient
.failJob({
errorMessage: `Unhandled exception in task handler ${e}`,
jobKey: job.key,
retries,
retryBackOff: 0,
})
.catch((e) => {
console.error(
'Any error was thrown while failing the job after an unhandled exception in the task handler'
)
console.error(e.message)
})
} catch (e: unknown) {
this.logger.logDebug(e)
} finally {
Expand Down

0 comments on commit fa368b5

Please sign in to comment.