🎥 Make videos programmatically with React
handling
Jonny Burger972261ae4c10
diff --git a/packages/docs/docs/media/audio.mdx b/packages/docs/docs/media/audio.mdxindex f2ec706db2..ac6f71d1e8 100644--- a/packages/docs/docs/media/audio.mdx+++ b/packages/docs/docs/media/audio.mdx@@ -115,34 +115,34 @@ A name and that will be shown as the label of the sequence in the timeline of th### `onError?`<AvailableFrom v="4.0.404" />-Handle errors that occur during audio loading. The callback receives an object with an `error` property and should return either `'fallback'` or `'fail'`.+Handle errors that occur during audio processing. The callback receives an `Error` and should return either `'fallback'` or `'fail'`.-- Return `'fallback'` to fall back to [`<Html5Audio>`](/docs/html5-audio) (default behavior when this prop is not specified)+- Return `'fallback'` to fall back to [`<Html5Audio>`](/docs/html5-audio) (default behavior)- Return `'fail'` to fail the render immediately```tsx twoslash-import {AbsoluteFill, staticFile} from 'remotion';import {Audio} from '@remotion/media';export const MyVideo = () => {return (- <AbsoluteFill>- <Audio- src={staticFile('audio.mp3')}- onError={({error}) => {- console.log('Audio error:', error.message);- // Return 'fail' to fail the render, or 'fallback' to use Html5Audio- return 'fallback';- }}- />- </AbsoluteFill>+ <Audio+ src={'https://remotion.media/audio.mp3'}+ onError={(error) => {+ console.log('Audio error:', error.message);++ // Return 'fail' to fail the render, or 'fallback' to use <Html5Audio>+ return 'fallback';+ }}+ />);};```:::note-This callback is invoked when an error occurs in the `@remotion/media` audio processing. If you return `'fallback'`, the component will fall back to [`<Html5Audio>`](/docs/html5-audio) which may have its own error handling via [`fallbackHtml5AudioProps.onError`](#onerror-1).-:::++- In client-side rendering, the render will always fail regardless of the return value.+- If you return `'fallback'`, the component will fall back to [`<Html5Audio>`](/docs/html5-audio) which may have its own error handling via [`fallbackHtml5AudioProps.onError`](#onerror-1).+ :::### `playbackRate?`<AvailableFrom v="4.0.354" />diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdxindex 0ee8e6f93c..97ad0cda14 100644--- a/packages/docs/docs/media/video.mdx+++ b/packages/docs/docs/media/video.mdx@@ -117,34 +117,35 @@ A name and that will be shown as the label of the sequence in the timeline of th### `onError?`<AvailableFrom v="4.0.404" />-Handle errors that occur during video loading. The callback receives an object with an `error` property and should return either `'fallback'` or `'fail'`.+Handle errors that occur during video processing. The callback receives an `Error` property and should return either `'fallback'` or `'fail'`.-- Return `'fallback'` to fall back to [`<OffthreadVideo>`](/docs/offthreadvideo) (default behavior when this prop is not specified)+- Return `'fallback'` to fall back to [`<OffthreadVideo>`](/docs/offthreadvideo) (default behavior)- Return `'fail'` to fail the render immediately```tsx twoslash-import {AbsoluteFill, staticFile} from 'remotion';+import {staticFile} from 'remotion';import {Video} from '@remotion/media';export const MyVideo = () => {return (- <AbsoluteFill>- <Video- src={staticFile('video.webm')}- onError={({error}) => {- console.log('Video error:', error.message);- // Return 'fail' to fail the render, or 'fallback' to use OffthreadVideo- return 'fallback';- }}- />- </AbsoluteFill>+ <Video+ src={'https://remotion.media/video.mp4'}+ onError={(error) => {+ console.log('Video error:', error.message);++ // Return 'fail' to fail the render, or 'fallback' to use <OffthreadVideo>+ return 'fallback';+ }}+ />);};```:::note-This callback is invoked when an error occurs in the `@remotion/media` video processing. If you return `'fallback'`, the component will fall back to [`<OffthreadVideo>`](/docs/offthreadvideo) which may have its own error handling via [`fallbackOffthreadVideoProps.onError`](#onerror-1).-:::++- In client-side rendering, the render will always fail regardless of the return value.+- If you return `'fallback'`, the component will fall back to [`<OffthreadVideo>`](/docs/offthreadvideo) which may have its own error handling via [`fallbackOffthreadVideoProps.onError`](#onerror-1).+ :::### `playbackRate?`<AvailableFrom v="4.0.354" />diff --git a/packages/media/src/on-error.ts b/packages/media/src/on-error.tsnew file mode 100644index 0000000000..49f4cb5e0b--- /dev/null+++ b/packages/media/src/on-error.ts@@ -0,0 +1,36 @@+export type MediaErrorEvent = {+ error: Error;+};++export type MediaErrorAction = 'fallback' | 'fail';+export type MediaOnError = (error: Error) => MediaErrorAction | undefined;++export const callOnErrorAndResolve = ({+ onError,+ error,+ disallowFallback,+ isClientSideRendering,+ clientSideError,+}: {+ onError: MediaOnError | undefined;+ error: Error;+ clientSideError: Error;+ disallowFallback: boolean;+ isClientSideRendering: boolean;+}): [MediaErrorAction, Error] => {+ const result = onError?.(error);++ if (isClientSideRendering) {+ return ['fail', clientSideError];+ }++ if (result) {+ return [result, error];+ }++ if (disallowFallback) {+ return ['fail', error];+ }++ return ['fallback', error];+};