From 8bf7f0f151a6b9d417457dc94d796405fc0cef55 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sat, 22 Feb 2025 19:47:15 -0800 Subject: [PATCH] improve errors --- src/mapgen.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/mapgen.ts b/src/mapgen.ts index 6debb86..781d9db 100644 --- a/src/mapgen.ts +++ b/src/mapgen.ts @@ -99,6 +99,11 @@ export function generateMap(): LoadedNewMap { if (e instanceof TryAgainException) { continue; } + if (e instanceof BadMapError) { + console.log(`Bad map generated: ${e.message}:`); + showDebug(e.badMap); + continue; + } throw e; } } @@ -108,7 +113,7 @@ export function tryGenerateMap(vaultTemplates: VaultTemplate[]): LoadedNewMap { let width = WIDTH; let height = HEIGHT; if (width % 2 == 0 || height % 2 == 0) { - throw "must be odd-sized"; + throw new Error("map bounds must be odd-sized"); } let grid = new LoadedNewMap("generated", new Size(width, height)); @@ -498,7 +503,7 @@ function connectRegions(knife: Knife) { let sources: number[] = dedup(basicRegions.map((i) => merged[i])); let dest: number | undefined = sources.pop(); if (dest == undefined) { - throw "each connector should touch more than one region"; + throw new BadMapError(`each connector should touch more than one region but ${connector} does not`, knife.map); } if (Math.random() > EXTRA_CONNECTOR_CHANCE) { @@ -624,7 +629,7 @@ function decorateRoom(_map: LoadedNewMap, _rect: Rect) {} function randrange(lo: number, hi: number) { if (lo >= hi) { - throw `randrange: hi must be >= lo, ${hi}, ${lo}`; + throw new Error(`randrange: hi must be >= lo, ${hi}, ${lo}`); } return lo + Math.floor(Math.random() * (hi - lo)); @@ -658,3 +663,11 @@ function showDebug(grid: LoadedNewMap) { } class TryAgainException extends Error {} +class BadMapError extends Error{ + badMap: LoadedNewMap; + + constructor(msg: string, badMap: LoadedNewMap) { + super(msg); + this.badMap = badMap; + } +}