From 95dccaeb57d570bf4a162b4a208917650fff8eb7 Mon Sep 17 00:00:00 2001 From: HerozDotExe Date: Mon, 16 Mar 2026 18:13:49 +0100 Subject: [PATCH] feat: add cleaner error handling and redact token in errors --- src/launcher/instance.ts | 37 ++++----------- src/launcher/java.ts | 4 +- src/utils/errors.ts | 52 +++++++++++++++++++--- src/utils/types.ts | 2 +- tests/launcher/modloaders/oldForge.test.ts | 10 ++--- 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/src/launcher/instance.ts b/src/launcher/instance.ts index e1ffe4e..aa8c9f3 100644 --- a/src/launcher/instance.ts +++ b/src/launcher/instance.ts @@ -9,7 +9,7 @@ import { import * as core from "../core"; import path from "path"; import { EventEmitter } from "node:events"; -import { InstallError, LaunchError } from "../utils/errors"; +import { throwLaunch, throwInstall } from "../utils/errors"; import { installForge } from "../core/modloaders"; import { readJson } from "../utils/fs"; import { mergeManifests } from "../core/mergeManifests"; @@ -105,8 +105,7 @@ export class Instance extends EventEmitter { this.versionLocation!, ); } catch (original) { - const error = new InstallError("installInit", original); - error.throw(); + throwInstall("installInit", original, this) } try { @@ -127,8 +126,7 @@ export class Instance extends EventEmitter { }); await librariesDownloader.run(); } catch (original) { - const error = new InstallError("libraries", original); - error.throw(); + throwInstall("libraries", original, this) } try { @@ -148,8 +146,7 @@ export class Instance extends EventEmitter { }); await assetsDownloader.run(); } catch (original) { - const error = new InstallError("assets", original); - error.throw(); + throwInstall("assets", original, this) } try { @@ -169,14 +166,12 @@ export class Instance extends EventEmitter { }); await nativesDownloader.run(); } catch (original) { - const error = new InstallError("natives", original); - error.throw(); + throwInstall("natives", original, this) } const javaError = await checkJava(this.javaExecutable!) if (javaError) { - const error = new InstallError("java", javaError) - error.throw() + throwInstall("java", javaError, this) } if (this.modloader) { @@ -198,8 +193,7 @@ export class Instance extends EventEmitter { throw new Error("Unknown modloader"); } } catch (original) { - const error = new InstallError("modloader", original); - error.throw(); + throwInstall("modloader", original, this) } } } @@ -210,8 +204,7 @@ export class Instance extends EventEmitter { try { await this.initialize(); } catch (original) { - const error = new InstallError("launchInit", original); - error.throw(); + throw throwLaunch(original, this) } } @@ -269,19 +262,7 @@ export class Instance extends EventEmitter { return process; } catch (original) { - const error = new LaunchError( - { - version: this.version!, - auth: this.auth!, - paths: this.paths!, - customGameArgs: this.args.game!, - customJvmArgs: this.args.java!, - versionManifest: this.versionManifest!, - modloader: this.modloader!, - }, - original as Error, - ); - throw error.throw(); + throw throwLaunch(original, this) } } } diff --git a/src/launcher/java.ts b/src/launcher/java.ts index c891261..b588e7a 100644 --- a/src/launcher/java.ts +++ b/src/launcher/java.ts @@ -157,8 +157,8 @@ export class RuntimeManager extends EventEmitter { }); await javaDownloader.run(); } catch (original) { - const error = new InstallError("java", original); - error.throw(); + //@ts-expect-error no access to instance + new InstallError("java", null, original, "An error happened while downloading java.") } } diff --git a/src/utils/errors.ts b/src/utils/errors.ts index a9fcae7..0e8549e 100644 --- a/src/utils/errors.ts +++ b/src/utils/errors.ts @@ -1,14 +1,23 @@ import { version as packageVersion } from "../../package.json"; -import { LaunchErrorInfos } from "./types"; +import { Instance } from "../launcher"; +import { LaunchErrorConfig } from "./types"; + +function redactToken(config: LaunchErrorConfig) { + config.auth.access_token = "token" + config.auth.client_token = "token" + return config +} export class InstallError extends Error { + config: LaunchErrorConfig; step: string; original: Error; moreInfo?: string; - constructor(step: string, original: unknown, moreInfo?: string) { + constructor(step: string, config: LaunchErrorConfig, original: unknown, moreInfo?: string) { super(); this.step = step; this.original = original as Error; + this.config = config; this.moreInfo = moreInfo; } @@ -22,16 +31,18 @@ export class InstallError extends Error { console.error(this.moreInfo); } + console.dir(redactToken(this.config)); + throw this; } } export class LaunchError extends Error { - infos: LaunchErrorInfos; + config: LaunchErrorConfig; original: Error; - constructor(infos: LaunchErrorInfos, original: Error) { + constructor(config: LaunchErrorConfig, original: Error) { super(); - this.infos = infos; + this.config = config; this.original = original; } @@ -40,8 +51,37 @@ export class LaunchError extends Error { `[nlk ${packageVersion}] Error occured when launching the game: ${this.original.message}`, ); console.error(`[nlk ${packageVersion}] Original error is:`, this.original); - console.dir(this.infos); + console.dir(redactToken(this.config)); throw this; } } + +export function throwInstall(step: string, original: unknown, config: Instance) { + const error = new InstallError(step, + { + version: config.version!, + auth: config.auth!, + paths: config.paths!, + customGameArgs: config.args.game!, + customJvmArgs: config.args.java!, + versionManifest: config.versionManifest!, + modloader: config.modloader!, + }, + original); + error.throw(); +} + +export function throwLaunch(original: unknown, config: Instance) { + const error = new LaunchError({ + version: config.version!, + auth: config.auth!, + paths: config.paths!, + customGameArgs: config.args.game!, + customJvmArgs: config.args.java!, + versionManifest: config.versionManifest!, + modloader: config.modloader!, + }, + original as Error); + throw error.throw(); +} \ No newline at end of file diff --git a/src/utils/types.ts b/src/utils/types.ts index 20a1564..f34cae3 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -191,7 +191,7 @@ export type Paths = { javaRoot?: string; }; -export type LaunchErrorInfos = { +export type LaunchErrorConfig = { version: string; paths: Paths; auth: Auth; diff --git a/tests/launcher/modloaders/oldForge.test.ts b/tests/launcher/modloaders/oldForge.test.ts index 869284f..7501032 100644 --- a/tests/launcher/modloaders/oldForge.test.ts +++ b/tests/launcher/modloaders/oldForge.test.ts @@ -4,8 +4,8 @@ import path from "path"; import fs from "fs/promises"; const gameRoot = path.join(import.meta.dirname, "..", "temp"); -await fs.rm(gameRoot, { recursive: true, force: true }); -await fs.mkdir(gameRoot, { recursive: true }); +// await fs.rm(gameRoot, { recursive: true, force: true }); +// await fs.mkdir(gameRoot, { recursive: true }); test("launch game", { timeout: 0 }, async () => { const instance = new Instance(); @@ -13,14 +13,14 @@ test("launch game", { timeout: 0 }, async () => { javaManager.on("progress", console.log) - const java = await javaManager.use(await getJavaComponent("1.12.2")) + // const java = await javaManager.use(await getJavaComponent("1.12.2")) const auth = offlineAuth("player"); - instance.setVersion("1.7.10"); + instance.setVersion("1.7.1001"); instance.setPaths(gameRoot); instance.setAuth(auth); - instance.setJavaExecutable(java) + instance.setJavaExecutable("java") instance.setModLoader("forge", "10.13.4.1614"); instance.on("progress", console.log);