diff --git a/src/models/file.js b/src/models/file.js index 1153994..7eff0f1 100644 --- a/src/models/file.js +++ b/src/models/file.js @@ -57,15 +57,16 @@ class File { // Emit a download.start event with the url and filepath emitter.emit('download.start', { url: this.url, filepath: this.filepath }); - // Handle file errors - file.on('error', (err) => { - fs.unlink(filepath, () => { - // Emit a download.error event with the error - emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' }); + return new Promise((resolve, reject) => { + // Handle file errors + file.on('error', (err) => { + fs.unlink(filepath, () => { + // Emit a download.error event with the error + emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' }); + }); + return reject(err.message); }); - }); - return new Promise((resolve, reject) => { request .get(url.href, (response) => { // Check if response is valid