Browse Source

Refactor error handling in File class to emit download.error event and reject promise

node16
Onja 12 months ago
parent
commit
03f3c74d27
  1. 15
      src/models/file.js

15
src/models/file.js

@ -57,15 +57,16 @@ class File {
// Emit a download.start event with the url and filepath // Emit a download.start event with the url and filepath
emitter.emit('download.start', { url: this.url, filepath: this.filepath }); emitter.emit('download.start', { url: this.url, filepath: this.filepath });
// Handle file errors return new Promise((resolve, reject) => {
file.on('error', (err) => { // Handle file errors
fs.unlink(filepath, () => { file.on('error', (err) => {
// Emit a download.error event with the error fs.unlink(filepath, () => {
emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' }); // 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 request
.get(url.href, (response) => { .get(url.href, (response) => {
// Check if response is valid // Check if response is valid

Loading…
Cancel
Save