Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Component Error Class & Icon name variable used. #188

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/core/components/Icon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ interface Props extends HTMLAttributes<"svg"> {
height?: number;
}

class AstroIconError extends Error {
public hint: string;

constructor(message: string) {
super(message);
}
}

const req = Astro.request;
const { name = "", title, ...props } = Astro.props;
const map = cache.get(req) ?? new Map();
Expand All @@ -30,9 +38,9 @@ const includeSymbol = i === 0;
let [setName, iconName] = (name as string).split(":");

if (!setName && iconName) {
const err = new Error(`Invalid "name" provided!`);
const err = new AstroIconError(`Invalid "name" provided!`);
if (import.meta.env.DEV) {
err.hint = `The provided value of "${name}" is invalid.\n\nDid you forget the icon set name? If you were attemping to reference a local icon, use the icon's name directly. (ie. "${providedIconName}")`;
err.hint = `The provided value of "${name}" is invalid.\n\nDid you forget the icon set name? If you were attemping to reference a local icon, use the icon's name directly. (ie. "${iconName}")`;
}
throw err;
}
Expand All @@ -45,7 +53,7 @@ if (!iconName) {

// Check if the local icon set exists
if (!icons[setName]) {
const err = new Error('Unable to load the "local" icon set!');
const err = new AstroIconError('Unable to load the "local" icon set!');
if (import.meta.env.DEV) {
err.hint =
'It looks like the "local" set was not loaded.\n\nDid you forget to create the icon directory or to update your config?';
Expand All @@ -55,7 +63,7 @@ if (!iconName) {

// Check if the icon is missing from the local collection
if (!(iconName in icons[setName].icons)) {
const err = new Error(`Unable to locate "${name}" icon!`);
const err = new AstroIconError(`Unable to locate "${name}" icon!`);
if (import.meta.env.DEV) {
err.hint = `The icon named "${iconName}" was not found in your local icon directory.\n\nDid you forget to configure your icon directory or make a typo?`;
}
Expand All @@ -67,7 +75,7 @@ const collection = icons[setName];

// Iconify collection not configured correctly
if (!collection) {
const err = new Error(`Unable to locate the "${setName}" icon set!`);
const err = new AstroIconError(`Unable to locate the "${setName}" icon set!`);
if (import.meta.env.DEV) {
if (sets.includes(setName)) {
err.hint = `It looks like the "${setName}" set was not loaded.\n\nDid you install the "@iconify-json/${setName}" dependency?`;
Expand All @@ -81,7 +89,7 @@ if (!collection) {
const iconData = getIconData(collection, iconName ?? setName);
// Missing icon from the icon collection
if (!iconData) {
const err = new Error(`Unable to locate "${name}" icon!`);
const err = new AstroIconError(`Unable to locate "${name}" icon!`);
if (import.meta.env.DEV) {
const [maybeStar] = include[setName];
if (maybeStar === "*" || include[setName].includes(iconName)) {
Expand Down