From c37e5bafa07e865da0111fcd98b1ddc3c32757a8 Mon Sep 17 00:00:00 2001 From: Lorenzo Lewis Date: Tue, 12 Sep 2023 13:56:59 -0600 Subject: [PATCH] Add error handling Signed-off-by: Lorenzo Lewis --- packages/starlight/user-components/LinkCard.astro | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/starlight/user-components/LinkCard.astro b/packages/starlight/user-components/LinkCard.astro index f9c50f30ba..5e647f1d7e 100644 --- a/packages/starlight/user-components/LinkCard.astro +++ b/packages/starlight/user-components/LinkCard.astro @@ -4,6 +4,7 @@ import Badge from '../components/Badge.astro'; import { BadgeConfigSchema, type BadgeUserConfig } from '../schemas/badge'; import { errorMap } from '../utils/error-map'; import type { HTMLAttributes } from 'astro/types'; +import { AstroError } from 'astro/errors'; interface Props extends Omit, 'title'> { title: string; @@ -13,7 +14,17 @@ interface Props extends Omit, 'title'> { const { title, badge, description, ...attributes } = Astro.props; -const badgeData = BadgeConfigSchema().parse(badge, { errorMap }); +const badgeData = BadgeConfigSchema().safeParse(badge, { errorMap }); + +if (!badgeData.success) { + throw new AstroError( + 'Invalid `badge` prop passed to `` for route `' + + Astro.url.pathname + + '`.\n\n' + + badgeData.error.issues.map((i) => i.message).join('\n'), + 'Find out more at https://starlight.astro.build/reference/frontmatter/#badge' + ); +} ---
@@ -26,7 +37,7 @@ const badgeData = BadgeConfigSchema().parse(badge, { errorMap }); badge && ( <> {' '} - + ) }