Skip to content

Commit

Permalink
feat: add DiscordComponentsError to signify errors
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 18, 2024
1 parent b1e6f07 commit b072bea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/components/discord-button/DiscordButton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { css, html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { DiscordComponentsError } from '../../util.js';
import LaunchIcon from '../svgs/LaunchIcon.js';
import type { DiscordButtonProps } from '../../types.js';

Expand Down Expand Up @@ -125,7 +126,7 @@ export class DiscordButton extends LitElement implements DiscordButtonProps {

public checkParentElement() {
if (this.parentElement?.tagName.toLowerCase() !== 'discord-action-row') {
throw new Error('All <discord-button> components must be direct children of <discord-action-row>.');
throw new DiscordComponentsError('All <discord-button> components must be direct children of <discord-action-row>.');
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export { DiscordUnderlined } from './components/discord-underlined/DiscordUnderl

/* EXPORTS END */
export { getConfig, setConfig } from './config.js';
export { DiscordComponentsError } from './util.js';
export type * from './types.js';

declare global {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ export const handleTimestamp = (value: DiscordTimestamp, useTime = false, hour24
export const IMAGE_EXTENSION = /\.(bmp|jpe?g|png|gif|webp|tiff)$/i;

export const validateImageExtension = (url: string) => {
if (!IMAGE_EXTENSION.test(url)) throw new Error(`The url of an image for discord-attachment should match the regex ${IMAGE_EXTENSION}`);
if (!IMAGE_EXTENSION.test(url))
throw new DiscordComponentsError(`The url of an image for discord-attachment should match the regex ${IMAGE_EXTENSION}`);
};

export const getGlobalEmojiUrl = (emojiName: string): Emoji | undefined => getConfig().emojis?.[emojiName];

export class DiscordComponentsError extends Error {
public constructor(message: string) {
super(message);
this.name = 'DiscordComponentsError';
}
}

0 comments on commit b072bea

Please sign in to comment.