Skip to content

Commit

Permalink
Add TypeScript definition (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 11, 2019
1 parent 0fc09a6 commit af1fa2f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
73 changes: 73 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
declare namespace unhandled {
interface UnhandledOptions {
/**
Custom logger that receives the error.
Can be useful if you for example integrate with Sentry.
@default console.error
*/
readonly logger?: (error: Error) => void;

/**
Present an error dialog to the user.
Default: [Only in production](https://github.com/sindresorhus/electron-is-dev).
*/
readonly showDialog?: boolean;

/**
When specified, the error dialog will include a `Report…` button, which when clicked, executes the given function with the error as the first argument.
@default undefined
@example
```
import unhandled = require('electron-unhandled');
import {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
```
*/
readonly reportButton?: (error: Error) => void;
}

interface LogErrorOptions {
/**
Title of the error dialog.
@default `${appName} encountered an error`
*/
readonly title?: string;
}
}

declare const unhandled: {
/**
Catch unhandled errors and promise rejections in your [Electron](https://electronjs.org) app.
You probably want to call this both in the main process and any renderer processes to catch all possible errors.
*/
(options?: unhandled.UnhandledOptions): void;

/**
* 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.
*
* @param error - Error to log.
*/
logError(error: Error, options?: unhandled.LogErrorOptions): void;
};

export = unhandled;
10 changes: 10 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expectType} from 'tsd';
import unhandled = require('.');

unhandled({logger: error => expectType<Error>(error)});
unhandled({logger: console.log});
unhandled({showDialog: true});
unhandled({reportButton: error => expectType<Error>(error)});

unhandled.logError(new Error());
unhandled.logError(new Error(), {title: 'foo'});
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
},
"scripts": {
"start": "electron example.js",
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"electron",
Expand All @@ -33,17 +34,18 @@
"debugging"
],
"dependencies": {
"clean-stack": "^2.0.0",
"clean-stack": "^2.1.0",
"electron-is-dev": "^1.0.1",
"ensure-error": "^1.0.0",
"lodash.debounce": "^4.0.8"
},
"devDependencies": {
"ava": "^0.25.0",
"ava": "^1.4.1",
"electron": "^3.0.6",
"electron-util": "^0.10.2",
"execa": "^1.0.0",
"xo": "^0.23.0"
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"xo": {
"nodeVersion": ">=10",
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const run = file => execa.stdout(electron, [file], {
});

test('error', async t => {
await t.notThrows(run('fixture-error.js'));
await t.notThrowsAsync(run('fixture-error.js'));
});

test('rejection', async t => {
await t.notThrows(run('fixture-rejection.js'));
await t.notThrowsAsync(run('fixture-rejection.js'));
});

0 comments on commit af1fa2f

Please sign in to comment.