Skip to content

🎥 Make videos programmatically with React

error handling for nonexistent files

Patric Salvisbergf3dad3c387ac
diff --git a/packages/cli/src/editor/components/Canvas.tsx b/packages/cli/src/editor/components/Canvas.tsx
index 6a556b2167..7a18362227 100644
--- a/packages/cli/src/editor/components/Canvas.tsx
+++ b/packages/cli/src/editor/components/Canvas.tsx
@@ -303,9 +303,13 @@ export const Canvas: React.FC<{
const fileType = getPreviewFileType(canvasContent.asset);
if (fileType === 'video') {
- await getVideoMetadata(assetSrc).then((data) => {
- setAssetResolution({width: data.width, height: data.height});
- });
+ try {
+ await getVideoMetadata(assetSrc).then((data) => {
+ setAssetResolution({width: data.width, height: data.height});
+ });
+ } catch {
+ setAssetResolution('none');
+ }
} else if (fileType === 'image') {
const img = new Image();
img.onload = () => {
diff --git a/packages/cli/src/editor/components/Preview.tsx b/packages/cli/src/editor/components/Preview.tsx
index c366018f58..d58dd7f980 100644
--- a/packages/cli/src/editor/components/Preview.tsx
+++ b/packages/cli/src/editor/components/Preview.tsx
@@ -1,6 +1,6 @@
import type {Size} from '@remotion/player';
import {PlayerInternals} from '@remotion/player';
-import React, {useContext, useEffect, useMemo, useRef, useState} from 'react';
+import React, {useContext, useEffect, useMemo, useRef} from 'react';
import type {CanvasContent} from 'remotion';
import {getStaticFiles, Internals, staticFile} from 'remotion';
import {formatBytes} from '../../format-bytes';
@@ -102,54 +102,33 @@ const containerStyle = (options: {
};
const AssetComponent: React.FC<{currentAsset: string}> = ({currentAsset}) => {
- const [fileSize, setFileSize] = useState<string>('');
const fileType = getPreviewFileType(currentAsset);
const staticFileSrc = staticFile(currentAsset);
const staticFiles = getStaticFiles();
- const [fileError, setFileError] = useState<string | null>(null);
const exists = staticFiles.find((file) => file.name === currentAsset);
- useEffect(() => {
- if (exists) {
- fetch(staticFileSrc).then((res) => {
- if (!res.ok) {
- throw new Error('An error occured when fetching the staticSrc');
- }
-
- if (!res.body) {
- return;
- }
-
- const reader = res.body.getReader();
- let totalBytes = 0;
-
- const readChunk: any = () => {
- return reader.read().then(({done, value}) => {
- if (done) {
- setFileSize(formatBytes(totalBytes));
- return;
- }
-
- totalBytes += value.length;
- return readChunk();
- });
- };
-
- return readChunk();
- });
- } else {
- setFileError(`${currentAsset} doesn't exist in your public folder`);
+
+ if (!exists) {
+ return (
+ <div style={{...msgStyle, color: FAIL_COLOR}}>
+ {currentAsset} does not exist in your public folder.
+ </div>
+ );
+ }
+
+ const fileSize = () => {
+ const fileFromStaticFiles = staticFiles.find(
+ (file) => file.name === currentAsset,
+ );
+ if (fileFromStaticFiles) {
+ return formatBytes(fileFromStaticFiles?.sizeInBytes);
}
- }, [currentAsset, exists, staticFileSrc]);
+ };
if (!currentAsset) {
return null;
}
- if (fileError) {
- return <div style={{...msgStyle, color: FAIL_COLOR}}>{fileError}</div>;
- }
-
if (fileType === 'audio') {
return (
<div>
@@ -178,7 +157,7 @@ const AssetComponent: React.FC<{currentAsset: string}> = ({currentAsset}) => {
<>
<div style={msgStyle}>{currentAsset}</div>
<Spacing y={1} />
- <div style={msgStyle}>Size: {fileSize} </div>
+ <div style={msgStyle}>Size: {fileSize()} </div>
</>
);
};