Skip to content

Commit

Permalink
Add logError method and reportButton option
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 11, 2018
1 parent b763595 commit f15bb29
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 38 deletions.
26 changes: 26 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const electron = require('electron');
const {openNewGitHubIssue, debugInfo} = require('electron-util');
const unhandled = require('.');

unhandled({
showDialog: true,
reportButton: error => {
openNewGitHubIssue({
user: 'sindresorhus',
repo: 'electron-unhandled',
body: `\`\`\`\n${error.stack}\n\`\`\`\n\n---\n\n${debugInfo()}`
});
}
});

let mainWindow;

(async () => {
await electron.app.whenReady();

mainWindow = new electron.BrowserWindow();
mainWindow.loadURL('https://google.com');

unhandled.logError(new Error('Test'));
})();
95 changes: 57 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,71 @@ const isDev = require('electron-is-dev');
const app = electron.app || electron.remote.app;
const dialog = electron.dialog || electron.remote.dialog;
const clipboard = electron.clipboard || electron.remote.clipboard;
const appName = app.getName();

let installed = false;

module.exports = options => {
if (installed) {
let options = {
logger: console.error,
showDialog: !isDev
};

const handleError = (title, error) => {
error = ensureError(error);

try {
options.logger(error);
} catch (loggerError) { // eslint-disable-line unicorn/catch-error-name
dialog.showErrorBox('The `logger` option function in electron-unhandled threw an error', ensureError(loggerError).stack);
return;
}

installed = true;
if (options.showDialog) {
const stack = cleanStack(error.stack);

options = {
logger: console.error,
showDialog: !isDev,
...options
};
if (app.isReady()) {
const buttons = [
'OK',
process.platform === 'darwin' ? 'Copy Error' : 'Copy error'
];

const handleError = (title, error) => {
error = ensureError(error);
if (options.reportButton) {
buttons.push('Report…');
}

try {
options.logger(error);
} catch (loggerError) { // eslint-disable-line unicorn/catch-error-name
dialog.showErrorBox('The `logger` option function in electron-unhandled threw an error', ensureError(loggerError).stack);
return;
}
// Intentionally not using the `title` option as it's not shown on macOS
const buttonIndex = dialog.showMessageBox({
type: 'error',
buttons,
defaultId: 0,
noLink: true,
message: title,
detail: cleanStack(error.stack, {pretty: true})
});

if (options.showDialog) {
const stack = cleanStack(error.stack);

if (app.isReady()) {
// Intentionally not using the `title` option as it's not shown on macOS
const buttonIndex = dialog.showMessageBox({
type: 'error',
buttons: [
'OK',
process.platform === 'darwin' ? 'Copy Error' : 'Copy error'
],
defaultId: 0,
noLink: true,
message: title,
detail: cleanStack(error.stack, {pretty: true})
});

if (buttonIndex === 1) {
clipboard.writeText(`${title}\n${stack}`);
}
} else {
dialog.showErrorBox(title, stack);
if (buttonIndex === 1) {
clipboard.writeText(`${title}\n${stack}`);
}

if (buttonIndex === 2) {
options.reportButton(error);
}
} else {
dialog.showErrorBox(title, stack);
}
}
};

module.exports = inputOptions => {
if (installed) {
return;
}

installed = true;

options = {
...options,
...inputOptions
};

if (process.type === 'renderer') {
Expand Down Expand Up @@ -86,3 +101,7 @@ module.exports = options => {
});
}
};

module.exports.logError = error => {
handleError(`${appName} encountered an error.`, error);
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"url": "sindresorhus.com"
},
"scripts": {
"start": "electron example.js",
"test": "xo && ava"
},
"files": [
Expand Down Expand Up @@ -40,6 +41,7 @@
"devDependencies": {
"ava": "^0.25.0",
"electron": "^3.0.6",
"electron-util": "^0.10.2",
"execa": "^1.0.0",
"xo": "^0.23.0"
},
Expand Down
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ Present an error dialog to the user.

<img src="screenshot.png" width="532">

#### reportButton

Type: `Function`<br>
Default: `undefined`

When specified, the error dialog will include a `Report…` button, which when clicked, executes the given function with the error as the first argument.

```js
const unhandled = require('electron-unhandled');
const {openNewGitHubIssue, debugInfo} = require('electron-util');

unhandled({
reportButton: error => {
openNewGitHubIssue({
user: 'sindresorhus',
repo: 'electron-unhandled',
body: `\`\`\`\n${error.stack}\n\`\`\`\n\n---\n\n${debugInfo()}`
});
}
});
```

[Example of how the GitHub issue will look like.](https://github.com/sindresorhus/electron-unhandled/issues/new?body=%60%60%60%0AError%3A+Test%0A++++at+%2FUsers%2Fsindresorhus%2Fdev%2Foss%2Felectron-unhandled%2Fexample.js%3A27%3A21%0A%60%60%60%0A%0A---%0A%0AExample+1.1.0%0AElectron+3.0.8%0Adarwin+18.2.0%0ALocale%3A+en-US)

### unhandled.logError(error)

Log an error. This does the same as with caught unhandled errors.

It will use the same options specified in the `unhandled()` call or the defaults.


## Related

Expand Down

0 comments on commit f15bb29

Please sign in to comment.