🎥 Make videos programmatically with React
propagete error
Jonny Burger3c715e730f0e
diff --git a/packages/renderer/rust/global_printer.rs b/packages/renderer/rust/global_printer.rsindex ce8cdaab11..1636cc7ead 100644--- a/packages/renderer/rust/global_printer.rs+++ b/packages/renderer/rust/global_printer.rs@@ -3,11 +3,11 @@ use std::io::{self, BufWriter, Write};use crate::errors::PossibleErrors;pub fn _print_debug(msg: &str) -> Result<(), PossibleErrors> {- synchronized_write_buf("0", &msg.as_bytes())+ synchronized_write_buf(0, "0", &msg.as_bytes())}-pub fn synchronized_write_buf(nonce: &str, data: &[u8]) -> Result<(), PossibleErrors> {- let str = format!("remotion_buffer:{};{}:", nonce, data.len());+pub fn synchronized_write_buf(status: i64, nonce: &str, data: &[u8]) -> Result<(), PossibleErrors> {+ let str = format!("remotion_buffer:{}:{}:{}:", nonce, data.len(), status,);let handle = io::stdout().lock();let mut stdout_guard = BufWriter::with_capacity(32 * 1024, handle);stdout_guard.write(str.as_bytes())?;diff --git a/packages/renderer/rust/main.rs b/packages/renderer/rust/main.rsindex fb604b937a..fc2430b89d 100644--- a/packages/renderer/rust/main.rs+++ b/packages/renderer/rust/main.rs@@ -35,7 +35,7 @@ fn mainfn() -> Result<(), PossibleErrors> {}_ => {let data = execute_command(opts.payload)?;- global_printer::synchronized_write_buf(&opts.nonce, &data)?;+ global_printer::synchronized_write_buf(0, &opts.nonce, &data)?;}}@@ -66,7 +66,7 @@ fn start_long_running_process() -> Result<(), PossibleErrors> {}let opts: CliInputCommand = parse_cli(&input).unwrap();pool.execute(move || match execute_command(opts.payload) {- Ok(res) => global_printer::synchronized_write_buf(&opts.nonce, &res).unwrap(),+ Ok(res) => global_printer::synchronized_write_buf(0, &opts.nonce, &res).unwrap(),Err(err) => {let err = ErrorPayload {error: error_to_string(err),@@ -76,7 +76,7 @@ fn start_long_running_process() -> Result<(), PossibleErrors> {let err_payload = serde_json::to_string(&err).unwrap();let j = err_payload.as_bytes();- global_printer::synchronized_write_buf(&opts.nonce, &j).unwrap()+ global_printer::synchronized_write_buf(1, &opts.nonce, &j).unwrap()}});}diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.tsindex 4bfe4d0f35..a1579c1e97 100644--- a/packages/renderer/src/compositor/compositor.ts+++ b/packages/renderer/src/compositor/compositor.ts@@ -2,7 +2,11 @@ import {spawn} from 'child_process';import {dynamicLibraryPathOptions} from '../call-ffmpeg';import {getExecutablePath} from './get-executable-path';import {makeNonce} from './make-nonce';-import type {CompositorCommand, CompositorCommandSerialized} from './payloads';+import type {+ CompositorCommand,+ CompositorCommandSerialized,+ ErrorPayload,+} from './payloads';export type Compositor = {finishCommands: () => void;@@ -76,13 +80,31 @@ export const startCompositor = <T extends keyof CompositorCommand>(const separator = Buffer.from('remotion_buffer:');const waiters = new Map<string, Waiter>();- const onMessage = (nonce: string, data: Buffer) => {+ const onMessage = (+ statusType: 'success' | 'error',+ nonce: string,+ data: Buffer+ ) => {if (nonce === '0') {console.log(data.toString('utf8'));}if (waiters.has(nonce)) {- (waiters.get(nonce) as Waiter).resolve(data);+ if (statusType === 'error') {+ try {+ const parsed = JSON.parse(data.toString('utf8')) as ErrorPayload;+ (waiters.get(nonce) as Waiter).reject(+ new Error(`Compositor error: ${parsed.error}`)+ );+ } catch (err) {+ (waiters.get(nonce) as Waiter).reject(+ new Error(data.toString('utf8'))+ );+ }+ } else {+ (waiters.get(nonce) as Waiter).resolve(data);+ }+waiters.delete(nonce);}};@@ -102,6 +124,7 @@ export const startCompositor = <T extends keyof CompositorCommand>(let nonceString = '';let lengthString = '';+ let statusString = '';// Each message from Rust is prefixed with `remotion_buffer;{[nonce]}:{[length]}`// Let's read the buffer to extract the nonce, and if the full length is available,@@ -110,8 +133,8 @@ export const startCompositor = <T extends keyof CompositorCommand>(// eslint-disable-next-line no-constant-conditionwhile (true) {const nextDigit = outputBuffer[separatorIndex];- // 0x3b is the character ";"- if (nextDigit === 0x3b) {+ // 0x3a is the character ":"+ if (nextDigit === 0x3a) {separatorIndex++;break;}@@ -126,6 +149,7 @@ export const startCompositor = <T extends keyof CompositorCommand>(const nextDigit = outputBuffer[separatorIndex];// 0x3a is the character ":"if (nextDigit === 0x3a) {+ separatorIndex++;break;}@@ -134,7 +158,21 @@ export const startCompositor = <T extends keyof CompositorCommand>(lengthString += String.fromCharCode(nextDigit);}+ // eslint-disable-next-line no-constant-condition+ while (true) {+ const nextDigit = outputBuffer[separatorIndex];+ // 0x3a is the character ":"+ if (nextDigit === 0x3a) {+ break;+ }++ separatorIndex++;++ statusString += String.fromCharCode(nextDigit);+ }+const length = Number(lengthString);+ const status = Number(statusString);const dataLength = outputBuffer.length - separatorIndex - 1;if (dataLength < length) {@@ -148,7 +186,7 @@ export const startCompositor = <T extends keyof CompositorCommand>(separatorIndex + 1,separatorIndex + 1 + Number(lengthString));- onMessage(nonceString, data);+ onMessage(status === 1 ? 'error' : 'success', nonceString, data);missingData = null;outputBuffer = outputBuffer.subarray(@@ -236,12 +274,8 @@ export const startCompositor = <T extends keyof CompositorCommand>(// TODO: Should have a way to error out a single taskchild.stdin.write(JSON.stringify(composed) + '\n');waiters.set(nonce, {- resolve: (data: Buffer) => {- resolve(data);- },- reject: (err) => {- reject(err);- },+ resolve,+ reject,});});},