Skip to content

fix: handle upstream photo fetch failures in demo endpoints#734

Open
AlexBill01 wants to merge 4 commits into
tempoxyz:mainfrom
AlexBill01:fix/photo-handlers-upstream-error
Open

fix: handle upstream photo fetch failures in demo endpoints#734
AlexBill01 wants to merge 4 commits into
tempoxyz:mainfrom
AlexBill01:fix/photo-handlers-upstream-error

Conversation

@AlexBill01

Copy link
Copy Markdown

Handle upstream photo fetch failures in demo endpoints

Description

Problem

The photo demo endpoints fetch picsum.photos after the MPP payment gate passes, but the fetch is never guarded:

if (result.status === 402) return result.challenge;

const res = await fetch("https://picsum.photos/1024/1024");
const url = res.url;

return result.withReceipt(Response.json({ url }));

If the upstream image host fails (timeout, 5xx, DNS/network error), the unhandled rejection surfaces as an opaque 500. Because the failure happens before withReceipt, no receipt is issued either, leaving a paid request in an ambiguous state with no actionable error.

Fix

Wrap the upstream fetch in try/catch, also checking res.ok. On failure, log and return a clear 502 Bad Gateway without issuing a receipt — so payment is not finalized for content that could not be delivered:

let url: string;
try {
  const res = await fetch("https://picsum.photos/1024/1024");
  if (!res.ok) throw new Error(`upstream responded ${res.status}`);
  url = res.url;
} catch (error) {
  console.error("[photo] upstream fetch failed:", error);
  return Response.json(
    { error: "Failed to load photo from upstream" },
    { status: 502 },
  );
}

The same guard is applied to all four photo endpoints (api/photo.ts, api/sessions/photo.ts, api/payment-link/photo.ts, api/payment-link/photo-stripe.ts). The HTML endpoints return a plain-text 502 body instead of JSON.

@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

@AlexBill01 is attempting to deploy a commit to the Tempo Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the docs label Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant