[Video] throw HLS errors to be caught by error boundary (#5166)

* throw HLS errors to be caught by error boundary

* wording tweak

* do the same on native

* fix type error
zio/stable
Samuel Newman 2024-09-05 16:03:00 +01:00 committed by GitHub
parent 60b74f7ab8
commit 428607d9a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 19 deletions

View File

@ -1,7 +1,7 @@
import React, {useCallback, useEffect, useId, useState} from 'react' import React, {useCallback, useEffect, useId, useState} from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {Image} from 'expo-image' import {Image} from 'expo-image'
import {VideoPlayerStatus} from 'expo-video' import {PlayerError, VideoPlayerStatus} from 'expo-video'
import {AppBskyEmbedVideo} from '@atproto/api' import {AppBskyEmbedVideo} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@ -78,6 +78,12 @@ function InnerWrapper({embed}: Props) {
(playerStatus === 'waitingToPlayAtSpecifiedRate' || (playerStatus === 'waitingToPlayAtSpecifiedRate' ||
playerStatus === 'loading') playerStatus === 'loading')
// send error up to error boundary
const [error, setError] = useState<Error | PlayerError | null>(null)
if (error) {
throw error
}
useEffect(() => { useEffect(() => {
if (isActive) { if (isActive) {
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
@ -92,10 +98,10 @@ function InnerWrapper({embed}: Props) {
) )
const statusSub = player.addListener( const statusSub = player.addListener(
'statusChange', 'statusChange',
(status, _oldStatus, error) => { (status, _oldStatus, playerError) => {
setPlayerStatus(status) setPlayerStatus(status)
if (status === 'error') { if (status === 'error') {
throw error setError(playerError ?? new Error('Unknown player error'))
} }
}, },
) )

View File

@ -1,13 +1,15 @@
import React, {useCallback, useEffect, useRef, useState} from 'react' import React, {useCallback, useEffect, useRef, useState} from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {AppBskyEmbedVideo} from '@atproto/api' import {AppBskyEmbedVideo} from '@atproto/api'
import {Trans} from '@lingui/macro' import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {clamp} from '#/lib/numbers' import {clamp} from '#/lib/numbers'
import {useGate} from '#/lib/statsig/statsig' import {useGate} from '#/lib/statsig/statsig'
import { import {
HLSUnsupportedError, HLSUnsupportedError,
VideoEmbedInnerWeb, VideoEmbedInnerWeb,
VideoNotFoundError,
} from '#/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerWeb' } from '#/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerWeb'
import {atoms as a} from '#/alf' import {atoms as a} from '#/alf'
import {ErrorBoundary} from '../ErrorBoundary' import {ErrorBoundary} from '../ErrorBoundary'
@ -152,23 +154,26 @@ function ViewportObserver({
} }
function VideoError({error, retry}: {error: unknown; retry: () => void}) { function VideoError({error, retry}: {error: unknown; retry: () => void}) {
const isHLS = error instanceof HLSUnsupportedError const {_} = useLingui()
let showRetryButton = true
let text = null
if (error instanceof VideoNotFoundError) {
text = _(msg`Video not found.`)
} else if (error instanceof HLSUnsupportedError) {
showRetryButton = false
text = _(
msg`Your browser does not support the video format. Please try a different browser.`,
)
} else {
text = _(msg`An error occurred while loading the video. Please try again.`)
}
return ( return (
<VideoFallback.Container> <VideoFallback.Container>
<VideoFallback.Text> <VideoFallback.Text>{text}</VideoFallback.Text>
{isHLS ? ( {showRetryButton && <VideoFallback.RetryButton onPress={retry} />}
<Trans>
Your browser does not support the video format. Please try a
different browser.
</Trans>
) : (
<Trans>
An error occurred while loading the video. Please try again later.
</Trans>
)}
</VideoFallback.Text>
{!isHLS && <VideoFallback.RetryButton onPress={retry} />}
</VideoFallback.Container> </VideoFallback.Container>
) )
} }

View File

@ -23,6 +23,12 @@ export function VideoEmbedInnerWeb({
const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false) const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false)
const figId = useId() const figId = useId()
// send error up to error boundary
const [error, setError] = useState<Error | null>(null)
if (error) {
throw error
}
const hlsRef = useRef<Hls | undefined>(undefined) const hlsRef = useRef<Hls | undefined>(undefined)
useEffect(() => { useEffect(() => {
@ -38,12 +44,25 @@ export function VideoEmbedInnerWeb({
// initial value, later on it's managed by Controls // initial value, later on it's managed by Controls
hls.autoLevelCapping = 0 hls.autoLevelCapping = 0
hls.on(Hls.Events.SUBTITLE_TRACKS_UPDATED, (event, data) => { hls.on(Hls.Events.SUBTITLE_TRACKS_UPDATED, (_event, data) => {
if (data.subtitleTracks.length > 0) { if (data.subtitleTracks.length > 0) {
setHasSubtitleTrack(true) setHasSubtitleTrack(true)
} }
}) })
hls.on(Hls.Events.ERROR, (_event, data) => {
if (data.fatal) {
if (
data.details === 'manifestLoadError' &&
data.response?.code === 404
) {
setError(new VideoNotFoundError())
} else {
setError(data.error)
}
}
})
return () => { return () => {
hlsRef.current = undefined hlsRef.current = undefined
hls.detachMedia() hls.detachMedia()
@ -104,3 +123,9 @@ export class HLSUnsupportedError extends Error {
super('HLS is not supported') super('HLS is not supported')
} }
} }
export class VideoNotFoundError extends Error {
constructor() {
super('Video not found')
}
}