From a1876015c5f8f4580e80aa9cda9b59b8199639c7 Mon Sep 17 00:00:00 2001 From: chrisbamonday Date: Tue, 19 May 2026 14:28:00 -0400 Subject: [PATCH] prevent SDK retry from crashing on circular errors --- src/queue-processors/base-queue-processor.ts | 9 ++++++++- src/utils/log-utils.ts | 21 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/queue-processors/base-queue-processor.ts b/src/queue-processors/base-queue-processor.ts index ad8de09..da6b5ce 100644 --- a/src/queue-processors/base-queue-processor.ts +++ b/src/queue-processors/base-queue-processor.ts @@ -121,7 +121,14 @@ export abstract class BaseQueueProcessor< return { status: "success" }; } catch (err) { const decision = this.getRetryDecision(err, job); - this.logProcessingError(jobId, err, decision); + try { + this.logProcessingError(jobId, err, decision); + } catch (logErr) { + console.error( + `[job:${jobId}] logProcessingError threw`, + logErr instanceof Error ? logErr.message : String(logErr), + ); + } if (decision.shouldRetry) { try { const attempts = await this.retryJob(job, decision.options); diff --git a/src/utils/log-utils.ts b/src/utils/log-utils.ts index 0f1e991..4aaaa4f 100644 --- a/src/utils/log-utils.ts +++ b/src/utils/log-utils.ts @@ -1,10 +1,27 @@ const JSON_INDENT = 2; +function getCircularReplacer(): (key: string, value: unknown) => unknown { + const seen = new WeakSet(); + return (_key, value) => { + if (typeof value === "object" && value !== null) { + if (seen.has(value as object)) { + return "[Circular]"; + } + seen.add(value as object); + } + return value; + }; +} + export function serializeError(detail: unknown): string { if (detail instanceof Error) { const { name, message, ...rest } = detail as Error & Record; - return JSON.stringify({ name, message, ...rest }, null, JSON_INDENT); + return JSON.stringify( + { name, message, ...rest }, + getCircularReplacer(), + JSON_INDENT, + ); } - return JSON.stringify(detail, null, JSON_INDENT); + return JSON.stringify(detail, getCircularReplacer(), JSON_INDENT); }