Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional onError callback options key #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
*/
readonly onCompleted?: (file: File) => void;

/**
Optional callback that receives an error title and an error message. It is called every time when download state is "interrupted".
If callback is not provided error will be shown in Electron error dialog.
*/
readonly onError?: (errorTitle: string, errorMessage: string) => void;

/**
Reveal the downloaded file in the system file manager, and if possible, select the file.

Expand Down Expand Up @@ -159,7 +165,7 @@
})();
```
*/
(options?: electronDl.Options): void;

Check failure on line 168 in index.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 14

Member call should be declared before all field definitions.

Check failure on line 168 in index.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 12

Member call should be declared before all field definitions.

/**
This can be useful if you need download functionality in a reusable module.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
if (typeof options.onCancel === 'function') {
options.onCancel(item);
}
callback(new CancelError());

Check failure on line 155 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 14

Expected blank line before this statement.

Check failure on line 155 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 12

Expected blank line before this statement.
} else if (state === 'interrupted') {
const message = pupa(errorMessage, {filename: path.basename(filePath)});
callback(new Error(message));
Expand Down Expand Up @@ -195,7 +195,11 @@
registerListener(session, options, (error, _) => {
if (error && !(error instanceof CancelError)) {
const errorTitle = options.errorTitle || 'Download Error';
dialog.showErrorBox(errorTitle, error.message);
if ('onError' in options && typeof options.onError === 'function') {
options.onError(errorTitle, error.message);
} else {
dialog.showErrorBox(errorTitle, error.message);
}
}
});
});
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ Optional callback that receives an object with information about an item that ha
}
```

#### onError

Type: `Function`

Optional callback that receives an error title and an error message. It is called every time when download state is "interrupted".
If callback is not provided error will be shown in Electron error dialog.
(*is applied only in default exported function, not for usage inside 'download' function)

#### openFolderWhenDone

Type: `boolean`\
Expand Down
Loading