A few changes to Watermark and Error Handling

- Made it so Watermark doesn't initialize on the constructor, this makes it possible for tests to use `@spt-aki/di/Container`;
- Removed unnecessary calls to `ErrorHandler.handleCriticalError`, all you really need to do is `throw new Error()` and it'll get caught automatically.
This commit is contained in:
TheSparta 2023-10-28 20:36:48 +01:00
parent ad5e0815b6
commit f354811ca8
4 changed files with 27 additions and 39 deletions

View File

@ -7,7 +7,7 @@ import { WinstonMainLogger } from "@spt-aki/utils/logging/WinstonMainLogger";
export class ErrorHandler
{
private logger:ILogger;
private logger: ILogger;
private readLine: readline.Interface;
constructor()
@ -19,15 +19,17 @@ export class ErrorHandler
});
}
public handleCriticalError(err: any): void
public handleCriticalError(err: Error): void
{
this.logger.error("The application had a critical error and failed to run");
this.logger.error(`Exception produced: ${err}`);
this.logger.error(`Exception produced: ${err.name}`);
if (err.stack)
this.logger.error(`\nStacktrace:\n ${err.stack}`);
{
this.logger.error(`\nStacktrace:\n${err.stack}`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.readLine.question("Press Enter to close the window", (_ans) => this.readLine.close());
this.readLine.on("close", () => process.exit(0));
this.readLine.on("close", () => process.exit(1));
}
}

View File

@ -18,26 +18,25 @@ export class Program
this.errorHandler = new ErrorHandler();
}
public start(): void
public async start(): Promise<void>
{
try
{
Container.registerTypes(container);
const childContainer = container.createChildContainer();
childContainer.resolve<Watermark>("Watermark");
const watermark = childContainer.resolve<Watermark>("Watermark");
watermark.initialize();
const preAkiModLoader = childContainer.resolve<PreAkiModLoader>("PreAkiModLoader");
Container.registerListTypes(childContainer);
preAkiModLoader.load(childContainer)
.then(() =>
{
await preAkiModLoader.load(childContainer);
Container.registerPostLoadTypes(container, childContainer);
childContainer.resolve<App>("App").load();
}).catch(rej => this.errorHandler.handleCriticalError(rej));
}
catch (e)
catch (err: any)
{
this.errorHandler.handleCriticalError(e);
this.errorHandler.handleCriticalError((err instanceof Error ? err : new Error(err)));
}
}
}

View File

@ -1,6 +1,5 @@
import { inject, injectable } from "tsyringe";
import { ErrorHandler } from "@spt-aki/ErrorHandler";
import { IPackageJsonData } from "@spt-aki/models/spt/mod/IPackageJsonData";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
@ -115,10 +114,6 @@ export class ModLoadOrder
if (visited.has(mod))
{
// Front: white, back: red
const errorMessage = this.localisationService.getText("modloader-cyclic_dependency");
this.logger.error(errorMessage);
// Additional info to help debug
this.logger.debug(this.localisationService.getText("modloader-checking_mod", mod));
this.logger.debug(`${this.localisationService.getText("modloader-checked")}:`);
@ -126,15 +121,12 @@ export class ModLoadOrder
this.logger.debug(`${this.localisationService.getText("modloader-visited")}:`);
this.logger.debug(JSON.stringify(visited, null, "\t"));
// Wait for input
const errorHandler = new ErrorHandler();
errorHandler.handleCriticalError({errorMessage});
throw new Error(this.localisationService.getText("modloader-cyclic_dependency"));
}
// Check dependencies
if (!this.modsAvailable.has(mod))
{
this.logger.error(this.localisationService.getText("modloader-missing_dependency"));
throw new Error(this.localisationService.getText("modloader-error_parsing_mod_load_order"));
}
@ -151,10 +143,7 @@ export class ModLoadOrder
{
if (this.modsAvailable.get(modAfter)?.loadAfter?.includes(mod))
{
const errorMessage = this.localisationService.getText("modloader-load_order_conflict", {modOneName: mod, modTwoName: modAfter});
this.logger.error(errorMessage);
const errorHandler = new ErrorHandler();
errorHandler.handleCriticalError(errorMessage);
throw new Error(this.localisationService.getText("modloader-load_order_conflict", {modOneName: mod, modTwoName: modAfter}));
}
dependencies.add(modAfter);

View File

@ -64,6 +64,8 @@ export class WatermarkLocale
export class Watermark
{
protected akiConfig: ICoreConfig;
protected text: string[] = [];
protected versionLabel = "";
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@ -73,16 +75,8 @@ export class Watermark
)
{
this.akiConfig = this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE);
this.initialize();
this.setTitle();
this.resetCursor();
this.draw();
}
protected text: string[] = [];
protected versionLabel = "";
public initialize(): void
{
const description = this.watermarkLocale.getDescription();
@ -103,6 +97,10 @@ export class Watermark
{
this.text = this.text.concat([...modding]);
}
this.setTitle();
this.resetCursor();
this.draw();
}
/**