|
1 | 1 | import React from "react"; |
2 | 2 | import { render } from "ink"; |
| 3 | +import { appendFileSync, mkdirSync, existsSync } from "fs"; |
| 4 | +import { join } from "path"; |
| 5 | +import { homedir } from "os"; |
3 | 6 | import { PacketStore, NodeStore } from "./protocol"; |
4 | 7 | import { App } from "./ui/App"; |
5 | 8 | import { initDb, clearDb, getDbPath } from "./db"; |
6 | 9 | import { getSetting, DEFAULT_MESHVIEW_URL } from "./settings"; |
7 | 10 |
|
| 11 | +// Global error handler - append errors to log file |
| 12 | +const ERROR_LOG_DIR = join(homedir(), ".config", "meshtastic-cli"); |
| 13 | +const ERROR_LOG_PATH = join(ERROR_LOG_DIR, "error.log"); |
| 14 | + |
| 15 | +function logError(type: string, error: Error | unknown) { |
| 16 | + try { |
| 17 | + if (!existsSync(ERROR_LOG_DIR)) { |
| 18 | + mkdirSync(ERROR_LOG_DIR, { recursive: true }); |
| 19 | + } |
| 20 | + const timestamp = new Date().toISOString(); |
| 21 | + const message = error instanceof Error |
| 22 | + ? `${error.message}\n${error.stack || ""}` |
| 23 | + : String(error); |
| 24 | + const entry = `\n[${timestamp}] ${type}\n${message}\n${"─".repeat(60)}\n`; |
| 25 | + appendFileSync(ERROR_LOG_PATH, entry); |
| 26 | + } catch { |
| 27 | + // Ignore logging errors |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +process.on("uncaughtException", (error) => { |
| 32 | + logError("UNCAUGHT EXCEPTION", error); |
| 33 | + console.error("Fatal error:", error.message); |
| 34 | + console.error(`Stack trace saved to ${ERROR_LOG_PATH}`); |
| 35 | + process.exit(1); |
| 36 | +}); |
| 37 | + |
| 38 | +process.on("unhandledRejection", (reason) => { |
| 39 | + logError("UNHANDLED REJECTION", reason); |
| 40 | +}); |
| 41 | + |
8 | 42 | // Parse CLI arguments |
9 | 43 | const args = process.argv.slice(2); |
10 | 44 | let address = "192.168.0.123"; |
|
0 commit comments