Skip to content

🎥 Make videos programmatically with React

rust error handling is done

Jonny Burger4aa6acbe8e1b
diff --git a/packages/renderer/src/compositor/compose.ts b/packages/renderer/src/compositor/compose.ts
index c99ae5991c..979eb3eb0c 100644
--- a/packages/renderer/src/compositor/compose.ts
+++ b/packages/renderer/src/compositor/compose.ts
@@ -6,6 +6,7 @@ import {dynamicLibraryPathOptions} from '../call-ffmpeg';
import {getExecutablePath} from './get-executable-path';
import {makeNonce} from './make-nonce';
import type {
+ CompositorCommand,
CompositorCommandSerialized,
CompositorImageFormat,
ErrorPayload,
@@ -23,6 +24,19 @@ const getCompositorHash = ({...input}: CompositorInput): string => {
return createHash('sha256').update(JSON.stringify(input)).digest('base64');
};
+export const serializeCommand = <Type extends keyof CompositorCommand>(
+ command: Type,
+ params: CompositorCommand[Type]
+): CompositorCommandSerialized<Type> => {
+ return {
+ nonce: makeNonce(),
+ payload: {
+ type: command,
+ params,
+ },
+ };
+};
+
export const compose = async ({
height,
width,
@@ -34,7 +48,6 @@ export const compose = async ({
downloadMap: DownloadMap;
output: string;
}) => {
- const bin = getExecutablePath('compositor');
const hash = getCompositorHash({height, width, layers, imageFormat});
if (downloadMap.compositorCache[hash]) {
@@ -42,27 +55,27 @@ export const compose = async ({
return;
}
- const payload: CompositorCommandSerialized<'Compose'> = {
- nonce: makeNonce(),
- payload: {
- type: 'Compose',
- params: {
- height,
- width,
- layers,
- output,
- output_format: imageFormat,
- },
- },
- };
+ const payload = serializeCommand('Compose', {
+ height,
+ width,
+ layers,
+ output,
+ output_format: imageFormat,
+ });
+
+ await callCompositor(JSON.stringify(payload));
- await new Promise<void>((resolve, reject) => {
+ downloadMap.compositorCache[hash] = output;
+};
+
+export const callCompositor = (payload: string) => {
+ return new Promise<void>((resolve, reject) => {
const child = spawn(
- bin,
- [JSON.stringify(payload)],
+ getExecutablePath('compositor'),
+ [payload],
dynamicLibraryPathOptions()
);
- child.stdin.write(JSON.stringify(payload));
+ child.stdin.write(payload);
child.stdin.end();
const stderrChunks: Buffer[] = [];
@@ -74,15 +87,18 @@ export const compose = async ({
} else {
const message = Buffer.concat(stderrChunks).toString('utf-8');
- const parsed = JSON.parse(message) as ErrorPayload;
-
- const err = new Error(parsed.error);
- err.stack = parsed.error + '\n' + parsed.backtrace;
+ try {
+ // Try to see if the error is a JSON
+ const parsed = JSON.parse(message) as ErrorPayload;
+ console.log({parsed});
+ const msg = `Compositor error: ${parsed.error}`;
+ const err = new Error(`${msg}\n${parsed.backtrace}`);
- reject(err);
+ reject(err);
+ } catch (err) {
+ reject(new Error(`Compositor panicked: ${message}`));
+ }
}
});
});
-
- downloadMap.compositorCache[hash] = output;
};
diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.ts
index f9e14f602e..22f5395b9b 100644
--- a/packages/renderer/src/compositor/compositor.ts
+++ b/packages/renderer/src/compositor/compositor.ts
@@ -1,6 +1,7 @@
import {spawn} from 'child_process';
import {dynamicLibraryPathOptions} from '../call-ffmpeg';
import {getActualConcurrency} from '../get-concurrency';
+import {serializeCommand} from './compose';
import {getExecutablePath} from './get-executable-path';
import {makeNonce} from './make-nonce';
import type {
@@ -35,13 +36,10 @@ export const startCompositor = <T extends keyof CompositorCommand>(
): Compositor => {
const bin = getExecutablePath('compositor');
- const fullCommand: CompositorCommandSerialized<T> = {
- nonce: makeNonce(),
- payload: {
- type,
- params: payload,
- },
- };
+ const fullCommand: CompositorCommandSerialized<T> = serializeCommand(
+ type,
+ payload
+ );
const child = spawn(
bin,
diff --git a/packages/renderer/src/test/rust-errors.test.ts b/packages/renderer/src/test/rust-errors.test.ts
index f687d957ed..a1cfdaad5e 100644
--- a/packages/renderer/src/test/rust-errors.test.ts
+++ b/packages/renderer/src/test/rust-errors.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'vitest';
+import {callCompositor, serializeCommand} from '../compositor/compose';
import {startLongRunningCompositor} from '../compositor/compositor';
test('Should get Rust errors in a good way', async () => {
@@ -50,4 +51,45 @@ test('Handle panics', async () => {
}
});
-test('Non-long running tasks should also be handled properly', () => {});
+test('Non-long running task panics should be handled', async () => {
+ const command = serializeCommand('DeliberatePanic', {});
+
+ try {
+ await callCompositor(JSON.stringify(command));
+ throw new Error('should not be reached');
+ } catch (err) {
+ expect((err as Error).message).toContain('Compositor panicked');
+ expect((err as Error).message).toContain("thread 'main' panicked");
+ }
+});
+
+test('Long running task failures should be handled', async () => {
+ const command = serializeCommand('ExtractFrame', {
+ input: 'fsdfds',
+ time: 1,
+ transparent: false,
+ });
+ try {
+ await callCompositor(JSON.stringify(command));
+ throw new Error('should not be reached');
+ } catch (err) {
+ expect((err as Error).message).toContain(
+ 'Compositor error: No such file or directory'
+ );
+ expect((err as Error).stack).toContain('opened_video::open_stream');
+ }
+});
+
+test('Invalid payloads will be handled', async () => {
+ // @ts-expect-error
+ const command = serializeCommand('ExtractFrame', {
+ input: 'fsdfds',
+ });
+ try {
+ await callCompositor(JSON.stringify(command));
+ } catch (err) {
+ expect((err as Error).message).toContain(
+ 'Compositor error: missing field `time`'
+ );
+ }
+});