From 6167dfa82fe52c2b8529fed50f43fda901482260 Mon Sep 17 00:00:00 2001 From: apemanzilla Date: Fri, 1 Dec 2017 11:05:57 -0500 Subject: [PATCH] Minor improvements and fixes --- package.json | 2 +- src/main.ts | 22 ++++++++++------------ src/utils/ErrorMapper.ts | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index c831885..f842563 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ }, "homepage": "https://github.com/screepers/screeps-typescript-starter#readme", "devDependencies": { - "@types/lodash": "^4.14.85", + "@types/lodash": "^3.10.1", "@types/source-map": "^0.5.2", "rollup": "^0.51.8", "rollup-plugin-clean": "^1.0.0", diff --git a/src/main.ts b/src/main.ts index 877401a..25d3bbe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,16 +1,14 @@ import { ErrorMapper } from "utils/ErrorMapper"; -export function loop() { - try { - // Clear non-existing creep memory. - for (const name in Memory.creeps) { - if (!Game.creeps[name]) { - delete Memory[name]; - } - } +// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change +// This utility uses source maps to get the line numbers and file names of the original, TS source code +export const loop = ErrorMapper.wrapLoop(function() { + console.log(`Current game tick is ${Game.time}`); - console.log(`Current tick is ${Game.time}`); - } catch (e) { - console.log(ErrorMapper.sourceMappedStackTrace(e)); + // Automatically delete memory of missing creeps + for (let name in Memory.creeps) { + if (!(name in Game.creeps)) { + delete Memory.creeps[name]; + } } -} +}); diff --git a/src/utils/ErrorMapper.ts b/src/utils/ErrorMapper.ts index 91475ab..405f44c 100644 --- a/src/utils/ErrorMapper.ts +++ b/src/utils/ErrorMapper.ts @@ -67,4 +67,23 @@ export class ErrorMapper { this.cache[stack] = outStack; return outStack; } + + public static wrapLoop(loop: () => void): () => void { + return () => { + try { + loop(); + } catch (e) { + if (e instanceof Error) { + if ("sim" in Game.rooms) { + console.log(`Source maps don't work in the simulator - displaying original error
${_.escape(e.stack)}
`) + } else { + console.log(`${_.escape(this.sourceMappedStackTrace(e))}`); + } + } else { + // can't handle it + throw e; + } + } + } + } }