Better exception handling for getDomain

This commit is contained in:
Fedor Indutny 2024-09-09 14:00:21 -07:00 committed by GitHub
parent 9efb046a06
commit 319890d138
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 9 deletions

View file

@ -9,7 +9,7 @@ import type { LinkPreviewType } from '../types/message/LinkPreviews';
import type { LocalizerType } from '../types/Util';
import { CurveType, Image } from './conversation/Image';
import { isImageAttachment } from '../types/Attachment';
import { getDomain } from '../types/LinkPreview';
import { getSafeDomain } from '../types/LinkPreview';
export type Props = LinkPreviewType & {
forceCompactMode?: boolean;
@ -26,7 +26,7 @@ export function StoryLinkPreview({
url,
}: Props): JSX.Element {
const isImage = isImageAttachment(image);
const location = domain || getDomain(String(url));
const location = domain || getSafeDomain(String(url));
const isCompact = forceCompactMode || !image;
let content: JSX.Element | undefined;
@ -74,7 +74,7 @@ export function StoryLinkPreview({
<div className="StoryLinkPreview__icon-container">
<Image
alt={i18n('icu:stagedPreviewThumbnail', {
domain: location,
domain: location || '',
})}
attachment={image}
curveBottomLeft={CurveType.Tiny}

View file

@ -12,7 +12,7 @@ import { Emojify } from './conversation/Emojify';
import { StoryLinkPreview } from './StoryLinkPreview';
import { TextAttachmentStyleType } from '../types/Attachment';
import { count } from '../util/grapheme';
import { getDomain } from '../types/LinkPreview';
import { getSafeDomain } from '../types/LinkPreview';
import { getFontNameByTextScript } from '../util/getFontNameByTextScript';
import {
COLOR_WHITE_INT,
@ -295,7 +295,7 @@ export const TextAttachment = forwardRef<HTMLTextAreaElement, PropsType>(
)}
<StoryLinkPreview
{...textAttachment.preview}
domain={getDomain(String(textAttachment.preview.url))}
domain={getSafeDomain(String(textAttachment.preview.url))}
forceCompactMode={getTextSize(textContent) !== TextSize.Large}
i18n={i18n}
title={textAttachment.preview.title || undefined}

View file

@ -4,7 +4,7 @@
import { createSelector } from 'reselect';
import { assertDev } from '../../util/assert';
import { getDomain } from '../../types/LinkPreview';
import { getSafeDomain } from '../../types/LinkPreview';
import type { LinkPreviewSourceType } from '../../types/LinkPreview';
import type { StateType } from '../reducer';
@ -21,7 +21,7 @@ export const getLinkPreview = createSelector(
return;
}
const domain = getDomain(linkPreview.url);
const domain = getSafeDomain(linkPreview.url);
assertDev(
domain !== undefined,
"Domain of linkPreview can't be undefined"

View file

@ -43,7 +43,11 @@ import type {
} from '../../components/conversation/GroupNotification';
import type { PropsType as ProfileChangeNotificationPropsType } from '../../components/conversation/ProfileChangeNotification';
import { getDomain, isCallLink, isStickerPack } from '../../types/LinkPreview';
import {
getSafeDomain,
isCallLink,
isStickerPack,
} from '../../types/LinkPreview';
import type {
AciString,
PniString,
@ -390,7 +394,7 @@ const getPreviewsForMessage = ({
...preview,
isStickerPack: isStickerPack(preview.url),
isCallLink: isCallLink(preview.url),
domain: getDomain(preview.url),
domain: getSafeDomain(preview.url),
image: preview.image ? getPropsForAttachment(preview.image) : undefined,
}));
};

View file

@ -146,6 +146,14 @@ export function findLinks(text: string, caretLocation?: number): Array<string> {
);
}
export function getSafeDomain(href: string): string | undefined {
try {
return getDomain(href);
} catch {
return undefined;
}
}
export function getDomain(href: string): string {
const url = maybeParseUrl(href);
if (!url || !url.hostname) {