Merge pull request #5681 from electron/default-app-error-handling

Tweak default app error handling
This commit is contained in:
Kevin Sawicki 2016-05-24 10:15:56 -07:00
commit 407a5d4415

View file

@ -1,6 +1,7 @@
const {app, dialog, shell, Menu} = require('electron') const {app, dialog, shell, Menu} = require('electron')
const fs = require('fs') const fs = require('fs')
const Module = require('module')
const path = require('path') const path = require('path')
const repl = require('repl') const repl = require('repl')
const url = require('url') const url = require('url')
@ -222,7 +223,7 @@ app.once('ready', () => {
}) })
if (option.modules.length > 0) { if (option.modules.length > 0) {
require('module')._preloadModules(option.modules) Module._preloadModules(option.modules)
} }
function loadApplicationPackage (packagePath) { function loadApplicationPackage (packagePath) {
@ -238,11 +239,13 @@ function loadApplicationPackage (packagePath) {
try { try {
packageJson = JSON.parse(fs.readFileSync(packageJsonPath)) packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
} catch (e) { } catch (e) {
showErrorMessage('Unable to parse package.json.\n\n' + showErrorMessage(`Unable to parse ${packageJsonPath}\n\n${e.message}`)
`${e.toString()} in ${packageJsonPath}`) return
} }
if (packageJson.version) app.setVersion(packageJson.version)
if (packageJson.version) {
app.setVersion(packageJson.version)
}
if (packageJson.productName) { if (packageJson.productName) {
app.setName(packageJson.productName) app.setName(packageJson.productName)
} else if (packageJson.name) { } else if (packageJson.name) {
@ -252,37 +255,26 @@ function loadApplicationPackage (packagePath) {
app.setPath('userCache', path.join(app.getPath('cache'), app.getName())) app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
app.setAppPath(packagePath) app.setAppPath(packagePath)
} }
const Module = require('module')
try { try {
Module._resolveFilename(packagePath, module, true) Module._resolveFilename(packagePath, module, true)
} catch (e) { } catch (e) {
showErrorMessage('Unable to find Electron app.\n\n' + showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
`See: ${packagePath}`) return
} }
// Run the app. // Run the app.
Module._load(packagePath, module, true) Module._load(packagePath, module, true)
} catch (e) { } catch (e) {
if (e.code === 'MODULE_NOT_FOUND') { console.error('App threw an error during load')
showErrorMessage('Unable to open Electron app.\n\n' + console.error(e.stack || e)
`${e.toString()}`) throw e
} else {
console.error('App threw an error when running', e)
throw e
}
} }
} }
function showErrorMessage (message) { function showErrorMessage (message) {
app.focus() app.focus()
dialog.showMessageBox({ dialog.showErrorBox('Error launching app', message)
message: 'Error opening app',
detail: message,
buttons: ['OK', 'Learn More']
}, (response) => {
if (response === 1) {
shell.openExternal('http://electron.atom.io/docs')
}
})
process.exit(1) process.exit(1)
} }
@ -313,12 +305,11 @@ if (option.file && !option.webdriver) {
console.log('v' + process.versions.electron) console.log('v' + process.versions.electron)
process.exit(0) process.exit(0)
} else if (option.help) { } else if (option.help) {
const helpMessage = `Electron v${process.versions.electron} - Cross Platform Desktop Application Shell const helpMessage = `Electron ${process.versions.electron} - Build cross platform desktop apps with JavaScript, HTML, and CSS
Usage: electron [options] [path] Usage: electron [options] [path]
A path to an Electron application may be specified. A path to an Electron app may be specified. The path must be one of the following:
The path must be one of the following:
- index.js file. - index.js file.
- Folder containing a package.json file. - Folder containing a package.json file.