diff --git a/server/api/og-image.ts b/server/api/og-image.ts index f0743b25..44ee9bf9 100644 --- a/server/api/og-image.ts +++ b/server/api/og-image.ts @@ -41,23 +41,32 @@ export default defineEventHandler(async (event) => { let ogImageUrl = '' - // First we want to try to get the og:image from the html - // But sometimes it is not included due to async JS loading - const html = await $fetch(cardUrl) - ogImageUrl = extractOgImageUrl(html) + try { + // First we want to try to get the og:image from the html + // But sometimes it is not included due to async JS loading + const html = await $fetch(cardUrl) + ogImageUrl = extractOgImageUrl(html) - if (process.env.NUXT_OPENGRAPH_API) { + if (process.env.NUXT_OPENGRAPH_API) { // If no og:image was found, try to get it from opengraph.io - if (!ogImageUrl) { - const response = await getOpenGraphClient().getSiteInfo(cardUrl) + if (!ogImageUrl) { + const response = await getOpenGraphClient().getSiteInfo(cardUrl) - ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || '' + ogImageUrl = response?.openGraph?.image?.url || response?.hybridGraph?.image || '' + } } + + // eslint-disable-next-line no-console + console.log(JSON.stringify({ cardUrl, ogImageUrl })) + + await send(event, ogImageUrl) + } + catch (error) { + throw createError({ + statusCode: 500, + statusMessage: (error as Error)?.message || 'Unknown error.', + cause: error, + }) } - - // eslint-disable-next-line no-console - console.log(JSON.stringify({ cardUrl, ogImageUrl })) - - await send(event, ogImageUrl) })