Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Lewis <lorenzo_lewis@icloud.com>
  • Loading branch information
lorenzolewis committed Sep 12, 2023
1 parent 8d0d7b9 commit c37e5ba
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/starlight/user-components/LinkCard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLAttributes<'a'>, 'title'> {
title: string;
Expand All @@ -13,7 +14,17 @@ interface Props extends Omit<HTMLAttributes<'a'>, '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 `<LinkCard>` 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'
);
}
---

<div>
Expand All @@ -26,7 +37,7 @@ const badgeData = BadgeConfigSchema().parse(badge, { errorMap });
badge && (
<>
{' '}
<Badge {...badgeData} />
<Badge {...badgeData.data} />
</>
)
}
Expand Down

0 comments on commit c37e5ba

Please sign in to comment.