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. 3
      src/models/file.js

3
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 });
return new Promise((resolve, reject) => {
// Handle file errors // Handle file errors
file.on('error', (err) => { file.on('error', (err) => {
fs.unlink(filepath, () => { fs.unlink(filepath, () => {
// Emit a download.error event with the error // Emit a download.error event with the error
emitter.emit('download.error', { url: this.url, filepath: this.filepath, error: err.message, type: 'file' }); 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