Skip to content

Commit

Permalink
feat(downloader): add downloader onError hook
Browse files Browse the repository at this point in the history
it provides information on tracking errors

closes #152
  • Loading branch information
nurrony committed Mar 22, 2024
1 parent 2eef599 commit df44c9d
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/Downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ class Downloader {
*/
onData = null;

/**
* @default null
* @type {Function | null}
* @description Function to track error
*/
onError = null;

/**
* @constructor
* @throws TypeError
Expand All @@ -149,16 +156,18 @@ class Downloader {
* @param {number} [downloderOptions.concurrency = 1] - concurrency limit to download playlist chunk
* @param {object} [downloderOptions.destination = ''] - Absolute path to download
* @param {object | Function} [downloderOptions.onData = null] - onData hook
* @param {object | Function} [downloderOptions.onError = null] - onError hook
* @param {boolean} [downloderOptions.overwrite = false] - Overwrite files toggler
* @param {object} [downloderOptions.options = {}] - Options to override from <a href="https://www.npmjs.com/package/ky" target="_blank">Ky</a>
* @throws ProtocolNotSupported
*/
constructor(
{ playlistURL, destination, concurrency = 1, overwrite = false, onData = null, ...options } = {
{ playlistURL, destination, concurrency = 1, overwrite = false, onData = null, onError = null, ...options } = {
concurrency: 1,
destination: '',
playlistURL: '',
onData: null,
onError: null,
overwrite: false,
options: {},
}
Expand All @@ -172,7 +181,9 @@ class Downloader {
this.pool = pLimit(concurrency ?? 1);
this.kyOptions = this.mergeOptions(options);
this.onData = onData;
// bind methods
this.onError = onError;

// method binding
this.fetchItems = this.fetchItems.bind(this);
this.downloadItem = this.downloadItem.bind(this);
this.mergeOptions = this.mergeOptions.bind(this);
Expand All @@ -190,6 +201,10 @@ class Downloader {
if (this.onData !== null && Utils.isNotFunction(this.onData)) {
throw TypeError('The `onData` must be a function');
}

if (this.onError !== null && Utils.isNotFunction(this.onError)) {
throw TypeError('The `onError` must be a function');
}
} catch (error) {
throw error;
}
Expand Down Expand Up @@ -280,6 +295,9 @@ class Downloader {
return { url, body };
} catch ({ name, message }) {
this.errors.push({ url, name, message });
if (this.onError) {
this.onError({ name, message, url });
}
return { url: '', body: '' };
}
}
Expand Down Expand Up @@ -330,6 +348,15 @@ class Downloader {
readStream.destroy();
writeStream.destroy();
unlink(filePath);

if (this.onError) {
this.onError({
url: item,
name: error.name,
message: error.message,
});
}

reject(error);
});

Expand All @@ -344,11 +371,21 @@ class Downloader {
writeStream.on('error', error => {
writeStream.destroy();
readStream.destroy();
if (this.onError) {
this.onError({
url: item,
name: error.name,
message: error.message,
});
}
reject(error);
});
});
} catch ({ name, message }) {
this.errors.push({ name, message, url: item });
if (this.onError) {
this.onError({ name, message, url: item });
}
}
}

Expand All @@ -370,6 +407,9 @@ class Downloader {
return Promise.allSettled(downloaderPromises);
} catch (error) {
this.errors.push({ url: this.playlistURL, name: error.name, message: error.message });
if (this.onError) {
this.onError({ url: this.playlistURL, name: error.name, message: error.message });
}
}
}

Expand Down

0 comments on commit df44c9d

Please sign in to comment.