GithubHelp home page GithubHelp logo

sindresorhus / electron-unhandled Goto Github PK

View Code? Open in Web Editor NEW
435.0 7.0 29.0 90 KB

Catch unhandled errors and promise rejections in your Electron app

License: MIT License

JavaScript 94.91% TypeScript 5.09%
electron electron-module

electron-unhandled's Introduction

electron-unhandled

Catch unhandled errors and promise rejections in your Electron app

You can use this module directly in both the main and renderer process.

Install

npm install electron-unhandled

Requires Electron 14 or later.

Usage

const unhandled = require('electron-unhandled');

unhandled();

API

unhandled(options?)

You probably want to call this both in the main process and any renderer processes to catch all possible errors.

Note: At minimum, this function must be called in the main process.

options

Type: object

Note: Options can only be specified in the main process.

logger

Type: Function
Default: console.error

Custom logger that receives the error.

Can be useful if you for example integrate with Sentry.

showDialog

Type: boolean
Default: Only in production

Present an error dialog to the user.

reportButton

Type: Function
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.

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.

unhandled.logError(error, [options])

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.

error

Type: Error

Error to log.

options

Type: object

title

Type: string
Default: ${appName} encountered an error

The title of the error dialog.

Related

electron-unhandled's People

Contributors

armaldio avatar bendingbender avatar erdkse avatar jmerle avatar raphinesse avatar richienb avatar sindresorhus avatar slapbox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

electron-unhandled's Issues

Allow localisation of the library

I think it would be nice to allow localisation of the library. Electron already provides system locale so this is just a matter of extracting and translating strings.

Would you accept PR on this?

Hide stack trace from logError

Hi, I am using the following code in order to customize the error message shown to the user when a http request fails.

req.on('error', () => {
                const error = new Error('Custom error message.');
                error.stack = null;
                unhandled.logError(error, {
                    title: 'Custom error title'
                });
            });

However, the error popup also shows a stack trace at the end of my message, that I would like to hide, is there any way I can achieve this?
image

Dialog message prefix

It would be nice if I could add a prefix to the dialog message.

I'd like the message to contain something like "Please contact me with on the website with the following error: ..."

I tried using logError manually but then I'd also have to recreate the dialog manually and that's not very DRY for a simple text prefix.

Duplicate dialogs and application still crashes

Iv added electron-unhandled to my app, although now I just get two dialogs, the default one, showing the stacktrace and error, and then the dialog from electron-unhandled, and then the application still crashes.

I just want to display the one dialog, from electron-unhandled and not have the app crash.

How can I do this?

Only show the error dialog in production by default?

Currently, the GUI error dialog is always shown. I'm thinking it might be better to only show it in production as in development you most likely have DevTools open already and having to click away an alert dialog is just an inconvenience. Thoughts?

Would use electron-is-dev to check whether the app is in production or not.

Add a filter option

It would be great if we had a configurable filter function that, if defined in the options, gets called BEFORE the options.logger and showMessageBox function get called and returns true or false to decide if the error should pass or not.

That way one could filter out error messages like ERR_CONNECTION_REFUSED that are not catchable

Register it automatically on new browser windows

Would be nice to have an opt-in option to have it register the handler on all new browser windows.

Could do something like this:

app.on('browser-window-created', (e, win) => {
  win.webContents.executeJavaScript('');
});

And execute some JS that includes it.

Unhandled Errors Message Information

Adding unhandled() to my electron application, when getting an error message I don't seem to get relevent helpful information as compared to the example. Instead I get a NonError, which I do not understand. The stack trace doesn't appear to reference any code I've written. This is for any error I get that pops up.

I appologise if this is working as intended. If so then some clarification would be appreciated!

I am using Electron 7, with Vue & Vue-Router. I have added unhandled in my main.js and my App.vue.

Example error:

Unhandled Error
NonError: ErrorEvent { isTrusted: [Getter] }
    at module.exports (C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/ensure-error/index.js:14:10)
    at handleError (C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/electron-unhandled/index.js:24:10)
    at C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/electron-unhandled/index.js:83:4
    at invokeFunc (C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/lodash.debounce/index.js:160:19)
    at trailingEdge (C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/lodash.debounce/index.js:207:14)
    at timerExpired (C:/Users/DefProc Engineering/AppData/Local/Programs/gh-record/resources/app.asar/node_modules/lodash.debounce/index.js:195:14)

Does not work without webpack polyfills in renderer mode

Error message:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
	- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "path": false }
resolve 'path' in '/Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron'
  Parsed request is a module
  using description file: /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules
        single file module
          using description file: /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/package.json (relative path: ./node_modules/path)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path.js doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path.ts doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path.jsx doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path.tsx doesn't exist
            .css
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path.css doesn't exist
        /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/electron/node_modules/path doesn't exist
      /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/node_modules doesn't exist or is not a directory
      looking for modules in /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules
        single file module
          using description file: /Users/mchalyi/Projects/uu3jrd/file-messenger/package.json (relative path: ./node_modules/path)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path.js doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path.ts doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path.jsx doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path.tsx doesn't exist
            .css
              Field 'browser' doesn't contain a valid alias configuration
              /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path.css doesn't exist
        /Users/mchalyi/Projects/uu3jrd/file-messenger/node_modules/path doesn't exist
      /Users/mchalyi/Projects/uu3jrd/node_modules doesn't exist or is not a directory
      /Users/mchalyi/Projects/node_modules doesn't exist or is not a directory
      /Users/mchalyi/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
 @ ./node_modules/electron-unhandled/index.js 2:33-52 15:23-42 35:19-38
 @ ./src/renderer/index.tsx 18:45-74

So basically the problem here is that node APIs are not polyfilled any more. I guess recommended way to fix this, would be to extract part which is intended for rendrer, to something like unhandledRenderer and do not import what is not needed.

Would you accept PR on such change? This is super breaking change.

Plans to remove use of the remote module?

contextIsolation is about to become the default setting with Electron@12. Remote is already disabled by default and will be removed in, I believe, Electron@14.

Are there plans to support these changes?

Thanks for sharing your great work @sindresorhus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.