1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00

Simplify pointless try/catch wrappers

Remove try/catch wrappers that just log and/or rethrow the error, use
the cause chain for rethrows with descriptions. Remove inner try/catch
for mod loading to make errors more obvious.
This commit is contained in:
Даниїл Григор'єв 2025-07-01 05:03:34 +03:00
parent 515c07c067
commit ca5dd9b3e5
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
5 changed files with 10 additions and 31 deletions

View File

@ -52,7 +52,7 @@ export class Application {
try { try {
await MODS.initMods(); await MODS.initMods();
} catch (ex) { } catch (ex) {
alert("Failed to load mods (launch with --dev for more info): \n\n" + ex); throw new Error("Failed to initialize mods", { cause: ex });
} }
this.unloaded = false; this.unloaded = false;

View File

@ -52,11 +52,7 @@ export class AnimationFrame {
dt = resetDtMs; dt = resetDtMs;
} }
try { this.frameEmitted.dispatch(dt);
this.frameEmitted.dispatch(dt);
} catch (ex) {
console.error(ex);
}
this.lastTime = time; this.lastTime = time;
window.requestAnimationFrame(this.boundMethod); window.requestAnimationFrame(this.boundMethod);
} }

View File

@ -95,12 +95,8 @@ export class StateManager {
const dialogParent = document.createElement("div"); const dialogParent = document.createElement("div");
dialogParent.classList.add("modalDialogParent"); dialogParent.classList.add("modalDialogParent");
document.body.appendChild(dialogParent); document.body.appendChild(dialogParent);
try {
this.currentState.internalEnterCallback(payload); this.currentState.internalEnterCallback(payload);
} catch (ex) {
console.error(ex);
throw ex;
}
this.app.sound.playThemeMusic(this.currentState.getThemeMusic()); this.app.sound.playThemeMusic(this.currentState.getThemeMusic());

View File

@ -275,7 +275,7 @@ function generateUpgrades() {
try { try {
ShapeDefinition.fromShortKey(shape); ShapeDefinition.fromShortKey(shape);
} catch (ex) { } catch (ex) {
throw new Error("Invalid upgrade goal: '" + ex + "' for shape" + shape); throw new Error("Invalid upgrade goal for shape " + shape, { cause: ex });
} }
}); });
}); });
@ -305,7 +305,7 @@ export function generateLevelDefinitions() {
try { try {
ShapeDefinition.fromShortKey(shape); ShapeDefinition.fromShortKey(shape);
} catch (ex) { } catch (ex) {
throw new Error("Invalid tutorial goal: '" + ex + "' for shape" + shape); throw new Error("Invalid tutorial goal for shape " + shape, { cause: ex });
} }
}); });
} }

View File

@ -2,7 +2,6 @@ import { GLOBAL_APP } from "@/core/globals";
import { SavegameStoredMods } from "@/savegame/savegame_typedefs"; import { SavegameStoredMods } from "@/savegame/savegame_typedefs";
import { createLogger } from "../core/logging"; import { createLogger } from "../core/logging";
import { DisabledMod } from "./disabled_mod"; import { DisabledMod } from "./disabled_mod";
import { ErroredMod } from "./errored_mod";
import { Mod, ModConstructor } from "./mod"; import { Mod, ModConstructor } from "./mod";
import { ModInfo, ModMetadata, ModQueueEntry } from "./mod_metadata"; import { ModInfo, ModMetadata, ModQueueEntry } from "./mod_metadata";
import { MOD_SIGNALS } from "./mod_signals"; import { MOD_SIGNALS } from "./mod_signals";
@ -84,22 +83,16 @@ export class ModLoader {
queue.map(async e => ({ entry: e, mod: await this.loadMod(e) })) queue.map(async e => ({ entry: e, mod: await this.loadMod(e) }))
); );
// Initialize all mods sequentially and collect errors // Initialize all mods sequentially
// TODO: Also collect early errors from the main process // TODO: Also collect early errors from the main process
for (const { entry, mod } of loadedMods) { for (const { entry, mod } of loadedMods) {
try {
await mod.init();
} catch (err) {
if (err instanceof Error) {
mod.errors.push(err);
}
}
this.mods.set(mod.id, { this.mods.set(mod.id, {
source: entry.source, source: entry.source,
file: entry.file, file: entry.file,
mod, mod,
}); });
await mod.init();
} }
} }
@ -139,13 +132,7 @@ export class ModLoader {
return new DisabledMod(entry.metadata, this.app, this); return new DisabledMod(entry.metadata, this.app, this);
} }
try { return await this.createModInstance(entry.metadata);
return await this.createModInstance(entry.metadata);
} catch (err) {
const mod = new ErroredMod(entry.metadata, this.app, this);
mod.errors.push(err instanceof Error ? err : new Error(err.toString()));
return mod;
}
} }
private async createModInstance(metadata: ModMetadata): Promise<Mod> { private async createModInstance(metadata: ModMetadata): Promise<Mod> {