From 03f3c74d27c901856a2d471a3ea26201c5845818 Mon Sep 17 00:00:00 2001 From: Onja Date: Tue, 10 Oct 2023 16:31:59 +0300 Subject: [PATCH] Refactor error handling in File class to emit download.error event and reject promise --- src/models/file.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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