From b9019af2ed0c78665bf908a5a8610df51edcee1d Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Tue, 8 Nov 2016 20:27:49 +0100 Subject: [PATCH] Better error handling Wrap in a bluebird.try to catch synchronous errors. bluebird.resolve doesn't catch those! --- src/ffmpeg.js | 67 +++++++++++++++++++++++------------------------- src/renderer.jsx | 11 ++++++-- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/ffmpeg.js b/src/ffmpeg.js index 2380ed6e..7f12a977 100644 --- a/src/ffmpeg.js +++ b/src/ffmpeg.js @@ -32,49 +32,46 @@ function getFfmpegPath() { } function cut(filePath, format, cutFrom, cutTo) { - const ext = path.extname(filePath) || format; - const outFileAppend = `${util.formatDuration(cutFrom)}-${util.formatDuration(cutTo)}`; - const outFile = `${filePath}-${outFileAppend}.${ext}`; + return bluebird.try(() => { + const ext = path.extname(filePath) || format; + const outFileAppend = `${util.formatDuration(cutFrom)}-${util.formatDuration(cutTo)}`; + const outFile = `${filePath}-${outFileAppend}.${ext}`; - console.log('Cutting from', cutFrom, 'to', cutTo); + console.log('Cutting from', cutFrom, 'to', cutTo); - const ffmpegArgs = [ - '-i', filePath, '-y', '-vcodec', 'copy', '-acodec', 'copy', - '-ss', cutFrom, '-t', cutTo - cutFrom, - '-f', format, - outFile, - ]; + const ffmpegArgs = [ + '-i', filePath, '-y', '-vcodec', 'copy', '-acodec', 'copy', + '-ss', cutFrom, '-t', cutTo - cutFrom, + '-f', format, + outFile, + ]; - console.log('ffmpeg', ffmpegArgs.join(' ')); + console.log('ffmpeg', ffmpegArgs.join(' ')); - return getFfmpegPath() - .then(ffmpegPath => execa(ffmpegPath, ffmpegArgs)) - .then((result) => { - console.log(result.stdout); - }) - .catch((err) => { - if (err.code === 1) { - alert('Whoops! ffmpeg was unable to cut this video. It may be of an unknown format or codec combination'); - return; - } - showFfmpegFail(err); - }); + return getFfmpegPath() + .then(ffmpegPath => execa(ffmpegPath, ffmpegArgs)) + .then((result) => { + console.log(result.stdout); + }); + }); } function getFormats(filePath) { - console.log('getFormat', filePath); + return bluebird.try(() => { + console.log('getFormat', filePath); - return getFfmpegPath() - .then(ffmpegPath => path.join(path.dirname(ffmpegPath), getWithExt('ffprobe'))) - .then(ffprobePath => execa(ffprobePath, [ - '-of', 'json', '-show_format', '-i', filePath, - ])) - .then((result) => { - const formatsStr = JSON.parse(result.stdout).format.format_name; - console.log('formats', formatsStr); - const formats = formatsStr.split(','); - return formats; - }); + return getFfmpegPath() + .then(ffmpegPath => path.join(path.dirname(ffmpegPath), getWithExt('ffprobe'))) + .then(ffprobePath => execa(ffprobePath, [ + '-of', 'json', '-show_format', '-i', filePath, + ])) + .then((result) => { + const formatsStr = JSON.parse(result.stdout).format.format_name; + console.log('formats', formatsStr); + const formats = formatsStr.split(','); + return formats; + }); + }); } module.exports = { diff --git a/src/renderer.jsx b/src/renderer.jsx index 1298fc4f..afdc9043 100644 --- a/src/renderer.jsx +++ b/src/renderer.jsx @@ -70,7 +70,7 @@ class App extends React.Component { resetState(); this.setState({ working: true }); - return bluebird.resolve(ffmpeg.getFormats(filePath)) + return ffmpeg.getFormats(filePath) .then((formats) => { if (formats.length < 1) return alert('Unsupported file'); return this.setState({ filePath, fileFormat: formats[0] }); @@ -188,7 +188,14 @@ class App extends React.Component { } this.setState({ working: true }); - return bluebird.resolve(ffmpeg.cut(filePath, this.state.fileFormat, cutStartTime, cutEndTime)) + return ffmpeg.cut(filePath, this.state.fileFormat, cutStartTime, cutEndTime) + .catch((err) => { + if (err.code === 1) { + alert('Whoops! ffmpeg was unable to cut this video. It may be of an unknown format or codec combination'); + return; + } + ffmpeg.showFfmpegFail(err); + }) .finally(() => this.setState({ working: false })); }