Better error messages for failed link cards
(cherry picked from commit 8cfdc094e1251b6d9490fabf80ae58c12241fcab)
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import {BskyAgent} from '@atproto/api'
|
import {BskyAgent} from '@atproto/api'
|
||||||
import {isBskyAppUrl} from '../strings/url-helpers'
|
import {msg} from '@lingui/macro'
|
||||||
import {extractBskyMeta} from './bsky'
|
import {I18nContext} from '@lingui/react'
|
||||||
|
|
||||||
import {LINK_META_PROXY} from 'lib/constants'
|
import {LINK_META_PROXY} from 'lib/constants'
|
||||||
import {getGiphyMetaUri} from 'lib/strings/embed-player'
|
import {getGiphyMetaUri} from 'lib/strings/embed-player'
|
||||||
|
import {isBskyAppUrl} from '../strings/url-helpers'
|
||||||
|
import {extractBskyMeta} from './bsky'
|
||||||
|
|
||||||
export enum LikelyType {
|
export enum LikelyType {
|
||||||
HTML,
|
HTML,
|
||||||
@@ -26,7 +29,8 @@ export interface LinkMeta {
|
|||||||
export async function getLinkMeta(
|
export async function getLinkMeta(
|
||||||
agent: BskyAgent,
|
agent: BskyAgent,
|
||||||
url: string,
|
url: string,
|
||||||
timeout = 15e3,
|
timeout: number,
|
||||||
|
i18n: I18nContext['i18n'],
|
||||||
): Promise<LinkMeta> {
|
): Promise<LinkMeta> {
|
||||||
if (isBskyAppUrl(url)) {
|
if (isBskyAppUrl(url)) {
|
||||||
return extractBskyMeta(agent, url)
|
return extractBskyMeta(agent, url)
|
||||||
@@ -58,9 +62,11 @@ export async function getLinkMeta(
|
|||||||
return meta
|
return meta
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
const to = setTimeout(() => controller.abort(), timeout || 5e3)
|
const signal = controller.signal
|
||||||
|
|
||||||
|
try {
|
||||||
|
const to = setTimeout(() => controller.abort('TIMEOUT'), timeout || 5e3)
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${LINK_META_PROXY(agent.service.toString() || '')}${encodeURIComponent(
|
`${LINK_META_PROXY(agent.service.toString() || '')}${encodeURIComponent(
|
||||||
@@ -82,9 +88,13 @@ export async function getLinkMeta(
|
|||||||
meta.image = image
|
meta.image = image
|
||||||
meta.title = title
|
meta.title = title
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// failed
|
if (signal.aborted) {
|
||||||
console.error(e)
|
meta.error = i18n._(
|
||||||
meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link'
|
msg`We were unable to fetch a preview for this URL because the request timed out.`,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
meta.error = i18n._(msg`We were unable to fetch a preview for this URL.`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return meta
|
return meta
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {useEffect, useState} from 'react'
|
import {useEffect, useState} from 'react'
|
||||||
|
import {useLingui} from '@lingui/react'
|
||||||
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {useFetchDid} from '#/state/queries/handle'
|
import {useFetchDid} from '#/state/queries/handle'
|
||||||
@@ -26,6 +27,7 @@ export function useExternalLinkFetch({
|
|||||||
}: {
|
}: {
|
||||||
setQuote: (opts: ComposerOpts['quote']) => void
|
setQuote: (opts: ComposerOpts['quote']) => void
|
||||||
}) {
|
}) {
|
||||||
|
const {i18n} = useLingui()
|
||||||
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
|
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
|
||||||
undefined,
|
undefined,
|
||||||
)
|
)
|
||||||
@@ -95,7 +97,7 @@ export function useExternalLinkFetch({
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
getLinkMeta(getAgent(), extLink.uri).then(meta => {
|
getLinkMeta(getAgent(), extLink.uri, 15e3, i18n).then(meta => {
|
||||||
if (aborted) {
|
if (aborted) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -137,7 +139,7 @@ export function useExternalLinkFetch({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
return cleanup
|
return cleanup
|
||||||
}, [extLink, setQuote, getPost, fetchDid, getAgent])
|
}, [extLink, setQuote, getPost, fetchDid, getAgent, i18n])
|
||||||
|
|
||||||
return {extLink, setExtLink}
|
return {extLink, setExtLink}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user