Skip to content

Commit

Permalink
Crash report #209
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed May 3, 2022
1 parent 91ab7c5 commit ee510f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
2 changes: 2 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const convert = require("./convert")
const time = require("./time")
const password = require("./password/index")
const localization = require("./localization")
const stack = require("./stack")

// ? export modules
module.exports = {
Expand All @@ -17,4 +18,5 @@ module.exports = {
time,
password,
localization,
stack,
}
39 changes: 39 additions & 0 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
/**
* Clean up Node.js stack trace
* @param {string} stack
* @return {string}
*/
clean: (stack) => {
const extractPathRegex = /\s+at.*[(\s](.*)\)?/
const pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/

if (typeof stack !== "string") {
return undefined
}

return stack
.replace(/\\/g, "/")
.split("\n")
.filter((line) => {
const pathMatches = line.match(extractPathRegex)
if (pathMatches === null || !pathMatches[1]) {
return true
}

const match = pathMatches[1]

// Electron (windows)
if (match.includes("node_modules/electron/dist/resources/electron.asar") || match.includes("node_modules/electron/dist/resources/default_app.asar")) {
return false
}

return !pathRegex.test(match)
})
.filter((line) => line.trim() !== "")
.map((line) => {
return line
})
.join("\n")
},
}
64 changes: 39 additions & 25 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ const os = require("os")
/**
* Catch crash
*/
process.on("uncaughtException", (error) => {
logger.error("Error on load", error.stack)
dialog.showErrorBox("Authme", `Authme crashed while starting. \n\nPlease open a GitHub Issue with a screenshot of this error (https://github.com/Levminer/authme). \n\n${error.stack}`)
process.on("uncaughtException", async (error) => {
const { stack } = require("@levminer/lib")

shell.openExternal("https://github.com/Levminer/authme/issues")
logger.error("Error on load", stack.clean(error.stack))
dialog.showErrorBox("Authme", `Authme crashed while starting, crash report sent. \n\nPlease restart Authme, if you want report this open a GitHub Issue with a screenshot of this error (https://github.com/Levminer/authme/issues). \n\n${stack.clean(error.stack)}`)

try {
await axios.post("https://api.levminer.com/api/v1/authme/analytics/post", {
type: "load_crash",
version: app.getVersion(),
build: number,
os: `${os.type()} ${os.arch()} ${os.release()}`,
stack: stack.clean(error.stack),
date: new Date(),
})
} catch (error) {
logger.error("Failed to send crash report", error)
}

process.crash()
})
Expand Down Expand Up @@ -391,6 +404,23 @@ const saveWindowPosition = () => {
saveSettings()
}

const crashReport = async (crash_type, error) => {
try {
await axios.post("https://api.levminer.com/api/v1/authme/analytics/post", {
type: crash_type,
version: authme_version,
build: build_number,
os: os_version,
hardware: os_info,
stack: error,
options: JSON.stringify(settings),
date: new Date(),
})
} catch (error) {
logger.error(error)
}
}

/**
* Create application windows
*/
Expand Down Expand Up @@ -847,26 +877,6 @@ app.whenReady()
.then(() => {
logger.log("App starting")

process.on("uncaughtException", async (error) => {
logger.error("Error occurred while starting", error.stack)

const result = await dialog.showMessageBox({
title: "Authme",
buttons: [lang.button.report, lang.button.close, lang.button.exit],
defaultId: 0,
cancelId: 1,
noLink: true,
type: "error",
message: `${lang.dialog.error} \n\n${error.stack}`,
})

if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
app.exit()
}
})

// Set application Id
if (dev === false) {
app.setAppUserModelId("Authme")
Expand Down Expand Up @@ -965,7 +975,9 @@ app.whenReady()
logger.log("App finished loading")
})
.catch((error) => {
logger.error("Error occurred while starting", error.stack)
logger.error("Error occurred while ready event", error.stack)

crashReport("start_crash", error.stack)

dialog
.showMessageBox({
Expand Down Expand Up @@ -1325,6 +1337,8 @@ ipc.on("rendererError", async (event, data) => {
message: `${lang.dialog.error} \n\n${data.error}`,
})

crashReport("renderer_crash", `Error in ${data.renderer}: ${data.error}`)

if (result.response === 0) {
shell.openExternal("https://github.com/Levminer/authme/issues/")
} else if (result.response === 2) {
Expand Down

0 comments on commit ee510f5

Please sign in to comment.