Skip to content

Commit 7483e46

Browse files
Miriadseniordeveloper
andcommitted
debug: add try/catch to OG endpoints to surface runtime errors
OG endpoints return 200 with image/png headers but 0-byte body. Adding error handling to blog.png and default.png to capture the actual WASM/font error from workers-og at runtime on CF Workers. Co-authored-by: seniordeveloper <seniordeveloper@miriad.systems>
1 parent 599e648 commit 7483e46

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

apps/web/src/pages/api/og/blog.png.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,34 @@ import { generateOgHtml, loadFonts, OG_CACHE_HEADER } from "../../../lib/og-util
1818
export const prerender = false;
1919

2020
export const GET: APIRoute = async ({ url }) => {
21-
const title = url.searchParams.get("title") || "CodingCat.dev";
22-
const author = url.searchParams.get("author") || "CodingCat.dev";
23-
const type = url.searchParams.get("type") || "Blog";
21+
try {
22+
const title = url.searchParams.get("title") || "CodingCat.dev";
23+
const author = url.searchParams.get("author") || "CodingCat.dev";
24+
const type = url.searchParams.get("type") || "Blog";
2425

25-
const html = generateOgHtml({ title, author, type });
26+
const html = generateOgHtml({ title, author, type });
27+
const fonts = loadFonts();
2628

27-
const response = new ImageResponse(html, {
28-
width: 1200,
29-
height: 630,
30-
fonts: loadFonts(),
31-
});
29+
const response = new ImageResponse(html, {
30+
width: 1200,
31+
height: 630,
32+
fonts,
33+
});
3234

33-
response.headers.set("Cache-Control", OG_CACHE_HEADER);
35+
response.headers.set("Cache-Control", OG_CACHE_HEADER);
3436

35-
return response;
37+
return response;
38+
} catch (error: any) {
39+
return new Response(
40+
JSON.stringify({
41+
error: error.message,
42+
stack: error.stack,
43+
name: error.name,
44+
}),
45+
{
46+
status: 500,
47+
headers: { "Content-Type": "application/json" },
48+
}
49+
);
50+
}
3651
};

apps/web/src/pages/api/og/default.png.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,32 @@ import { generateDefaultOgHtml, loadFonts, OG_CACHE_HEADER } from "../../../lib/
1818
export const prerender = false;
1919

2020
export const GET: APIRoute = async ({ url }) => {
21-
const title = url.searchParams.get("title") || "CodingCat.dev";
22-
const subtitle = url.searchParams.get("subtitle") || undefined;
21+
try {
22+
const title = url.searchParams.get("title") || "CodingCat.dev";
23+
const subtitle = url.searchParams.get("subtitle") || undefined;
2324

24-
const html = generateDefaultOgHtml({ title, subtitle });
25+
const html = generateDefaultOgHtml({ title, subtitle });
2526

26-
const response = new ImageResponse(html, {
27-
width: 1200,
28-
height: 630,
29-
fonts: loadFonts(),
30-
});
27+
const response = new ImageResponse(html, {
28+
width: 1200,
29+
height: 630,
30+
fonts: loadFonts(),
31+
});
3132

32-
response.headers.set("Cache-Control", OG_CACHE_HEADER);
33+
response.headers.set("Cache-Control", OG_CACHE_HEADER);
3334

34-
return response;
35+
return response;
36+
} catch (error: any) {
37+
return new Response(
38+
JSON.stringify({
39+
error: error.message,
40+
stack: error.stack,
41+
name: error.name,
42+
}),
43+
{
44+
status: 500,
45+
headers: { "Content-Type": "application/json" },
46+
}
47+
);
48+
}
3549
};

0 commit comments

Comments
 (0)