Skip to content

Commit

Permalink
handle unexpected downloader exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-m-song committed Jul 12, 2024
1 parent 56da9df commit 25c53b6
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/lib/Downloader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import path from 'path';

import { config, log } from '../config';
import { trimToJsonObject, tryParseJSON } from './utils';
import { config, log } from '../config';

export const downloaderCacheDir = path.join(config.cacheDir, 'ytdl');
export const downloaderOutputDir = path.join(config.cacheDir, 'out');
Expand Down Expand Up @@ -69,21 +69,19 @@ const execute = async (...args: string[]) => {
return null;
}

await new Promise<void>((resolve, reject) => {
child.on('close', (code, signal) => {
if (code !== 0) {
reject(new Error(`exit status was ${code}, signal was ${signal}`));
return;
}

resolve();
});

const exitCode = await new Promise<number | null>((resolve) => {
child.on('close', resolve);
child.on('error', (error) => log.error({ event: 'downloader', error }));
child.stdout.on('data', (chunk) => chunk?.toString && (stdout += chunk.toString()));
child.stderr.on('data', (chunk) => chunk?.toString && (stderr += chunk.toString()));
});

if (exitCode !== null && exitCode > 0) {
log.error({ event: 'downloader', stderr, exitCode, message: 'unexpected exit code' });
// continue anyway
// return null;
}

if (stderr.length > 0) {
log.error({ event: 'downloader', stderr, message: 'unexpected stderr output' });
// continue anyway
Expand Down

0 comments on commit 25c53b6

Please sign in to comment.