From 72a0194ca9fbf9ce3f6e00b03ec7345fd1962fb5 Mon Sep 17 00:00:00 2001 From: Sergiy Dybskiy Date: Mon, 28 Sep 2026 13:52:18 +0000 Subject: [PATCH] RefundOrder tool returns clear result for missing payment records Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> --- apps/storefront/lib/ai/tools.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/storefront/lib/ai/tools.ts b/apps/storefront/lib/ai/tools.ts index 9e2609e..91cfc2e 100644 --- a/apps/storefront/lib/ai/tools.ts +++ b/apps/storefront/lib/ai/tools.ts @@ -157,10 +157,15 @@ export function createTools(customerId: string, _conversationId?: string) { }; } - // selectPayment throws for orders that predate the payments launch — - // the demo's planted failure. Nothing catches it here on purpose: the - // AI SDK turns the rejection into a tool-error result. - const payment = await db.selectPayment(orderId); + let payment; + try { + payment = await db.selectPayment(orderId); + } catch (err) { + return { + refunded: false as const, + reason: err instanceof Error ? err.message : String(err), + }; + } const refund = await issueRefund({ chargeId: payment.chargeId, amount: order.total.amount,