@@ -83,6 +83,20 @@ function createWhatsAppReplyDeliveryReceipt(
|
83 | 83 | }); |
84 | 84 | } |
85 | 85 | |
| 86 | +function markWhatsAppVisibleDeliveryError(error: unknown): unknown { |
| 87 | +if (typeof error === "object" && error !== null && !Array.isArray(error)) { |
| 88 | +try { |
| 89 | +Object.assign(error, { sentBeforeError: true, visibleReplySent: true }); |
| 90 | +return error; |
| 91 | +} catch { |
| 92 | +// Fall back to a wrapper when a platform error object is non-extensible. |
| 93 | +} |
| 94 | +} |
| 95 | +const visibleError = new Error("visible WhatsApp reply delivery failed", { cause: error }); |
| 96 | +Object.assign(visibleError, { sentBeforeError: true, visibleReplySent: true }); |
| 97 | +return visibleError; |
| 98 | +} |
| 99 | + |
86 | 100 | export async function deliverWebReply(params: { |
87 | 101 | replyResult: ReplyPayload; |
88 | 102 | normalizedReplyResult?: DeliverableWhatsAppOutboundPayload<ReplyPayload>; |
@@ -150,15 +164,22 @@ export async function deliverWebReply(params: {
|
150 | 164 | }; |
151 | 165 | |
152 | 166 | const sendWithRetry = async <T>(fn: () => Promise<T>, label: string, maxAttempts = 3) => { |
153 | | -return await sendWhatsAppOutboundWithRetry({ |
154 | | -send: fn, |
155 | | - maxAttempts, |
156 | | -onRetry: ({ attempt, maxAttempts: retryMaxAttempts, backoffMs, errorText }) => { |
157 | | -logVerbose( |
158 | | -`Retrying ${label} to ${msg.from} after failure (${attempt}/${retryMaxAttempts - 1}) in ${backoffMs}ms: ${errorText}`, |
159 | | -); |
160 | | -}, |
161 | | -}); |
| 167 | +try { |
| 168 | +return await sendWhatsAppOutboundWithRetry({ |
| 169 | +send: fn, |
| 170 | + maxAttempts, |
| 171 | +onRetry: ({ attempt, maxAttempts: retryMaxAttempts, backoffMs, errorText }) => { |
| 172 | +logVerbose( |
| 173 | +`Retrying ${label} to ${msg.from} after failure (${attempt}/${retryMaxAttempts - 1}) in ${backoffMs}ms: ${errorText}`, |
| 174 | +); |
| 175 | +}, |
| 176 | +}); |
| 177 | +} catch (error: unknown) { |
| 178 | +if (sendResults.some((result) => result.providerAccepted)) { |
| 179 | +throw markWhatsAppVisibleDeliveryError(error); |
| 180 | +} |
| 181 | +throw error; |
| 182 | +} |
162 | 183 | }; |
163 | 184 | |
164 | 185 | // Text-only replies |
|