-
Notifications
You must be signed in to change notification settings - Fork 0
/
crasher.js
71 lines (57 loc) · 2.52 KB
/
crasher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
const { isBare, platform, arch } = require('which-runtime')
const fs = isBare ? require('bare-fs') : require('fs')
const path = isBare ? require('bare-path') : require('path')
const { CHECKOUT } = require('./constants')
const os = isBare ? require('bare-os') : require('os')
const pid = isBare ? global.Bare.pid : global.process.pid
let hasLoggedUnhandledRejection = false
let hasLoggedUncaughtException = false
const start = Date.now()
function logCrash (logPath, errorInfo, stackTrace, err) {
const timeStamp = (new Date()).toISOString()
const uptime = (Date.now() - start) / 1000
const driveInfo = `key=${CHECKOUT.key}\nlength=${CHECKOUT.length}\nfork=${CHECKOUT.fork}`
const processInfo = `platform=${platform}\narch=${arch}\npid=${pid}\nuptime=${uptime}s`
const errInfo = err !== null && typeof err === 'object' ? JSON.stringify(err, 0, 4).slice(1, -2) : ''
const errorMsg = `${timeStamp} ${errorInfo}\n${driveInfo}\n${processInfo}\nstack=${stackTrace + errInfo}\n\n`
console.error(errorMsg)
fs.writeFileSync(logPath, errorMsg, { flag: 'a', encoding: 'utf8' })
console.error(`Error logged at ${logPath}`)
}
function printCrash (errorInfo, stackTrace, err) {
const errInfo = err !== null && typeof err === 'object' ? JSON.stringify(err, 0, 4).slice(1, -2) : ''
const errorMsg = `${stackTrace + errInfo}\n\n${errorInfo}`
console.error(errorMsg)
}
function logAndExit (enableLog, logPath, errorInfo, stack, err) {
if (enableLog) {
logCrash(logPath, errorInfo, stack, err)
} else {
printCrash(errorInfo, stack, err)
}
if (isBare) {
os.kill(pid)
} else {
const runContext = global.process.versions.electron ? require('electron').app : global.process
runContext.exit(1)
}
}
function setupCrashHandlers (processName, swap, enableLog) {
const crashlogPath = path.join(swap, `${processName}.crash.log`)
const runContext = isBare ? global.Bare : global.process
runContext.on('unhandledRejection', (reason) => {
if (hasLoggedUnhandledRejection) return
hasLoggedUnhandledRejection = true
const stack = reason?.stack || reason || ''
const errorInfo = `${processName} exiting due to unhandled rejection`
logAndExit(enableLog, crashlogPath, errorInfo, stack, reason)
})
runContext.on('uncaughtException', (err) => {
if (hasLoggedUncaughtException) return
hasLoggedUncaughtException = true
const errorInfo = `${processName} exiting due to uncaught exception`
logAndExit(enableLog, crashlogPath, errorInfo, err.stack, err)
})
}
module.exports = setupCrashHandlers