feat: add cleaner error handling and redact token in errors
This commit is contained in:
parent
70427efd50
commit
95dccaeb57
5 changed files with 63 additions and 42 deletions
|
|
@ -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<InstanceEvents> {
|
|||
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<InstanceEvents> {
|
|||
});
|
||||
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<InstanceEvents> {
|
|||
});
|
||||
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<InstanceEvents> {
|
|||
});
|
||||
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<InstanceEvents> {
|
|||
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<InstanceEvents> {
|
|||
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<InstanceEvents> {
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,8 +157,8 @@ export class RuntimeManager extends EventEmitter<InstanceEvents> {
|
|||
});
|
||||
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.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -191,7 +191,7 @@ export type Paths = {
|
|||
javaRoot?: string;
|
||||
};
|
||||
|
||||
export type LaunchErrorInfos = {
|
||||
export type LaunchErrorConfig = {
|
||||
version: string;
|
||||
paths: Paths;
|
||||
auth: Auth;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue