javascript - 有没有办法将行号重新引入堆栈跟踪?
问题描述
我正在使用一个通过直接抛出对象来混淆错误的库 - 我相信。
//Functions a,b, b2, c are all in a library that I don't have control over.
function a() {
console.log("a");
}
function b() {
throw new Error("You can see my stacktrace!");
}
function b2() {
throw "I could have come from anywhere!";
}
function c() {
console.log("c");
}
//Our code here.
function all() {
try {
console.log("Start all()");
a();
b();
c();
} catch (err) {
console.error(err);
}
}
function all2() {
try {
console.log("Start all2()");
a();
b2();
c();
} catch (err) {
console.error(err);
}
}
all();
all2();
输出:
[nodemon] starting `node src/index.js localhost 8080`
Start all()
a
Error: You can see my stacktrace!
at b (/sandbox/src/index.js:7:9)
at all (/sandbox/src/index.js:23:5)
at Object.<anonymous> (/sandbox/src/index.js:41:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
Start all2()
a
I could have come from anywhere!
[nodemon] clean exit - waiting for changes before restart
问题是这让我很难调试我的代码 - 我不知道错误是来自函数a()
,b()
还是c()
.
有没有一种整洁的方法来重新引入堆栈跟踪?
到目前为止,我得到的最佳选择如下所示:
function wrapAndRunFunction(fn) {
try {
fn();
} catch (err) {
throw new Error(err);
}
}
function all3() {
try {
console.log("Start all3()");
wrapAndRunFunction(a);
wrapAndRunFunction(b2);
wrapAndRunFunction(c);
} catch (err) {
console.error(err);
}
}
Start all3()
a
Error: I could have come from anywhere!
at wrapAndRunFunction (/sandbox/src/index.js:45:11)
at all3 (/sandbox/src/index.js:53:5)
at Object.<anonymous> (/sandbox/src/index.js:62:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
[nodemon] clean exit - waiting for changes before restart
解决方案
推荐阅读
- sql - 我如何循环插入查询?
- c# - 相机移动时实例化游戏对象
- reactjs - AXIOS 发布请求 - 缺少 url 参数
- angular - 从核心 API 返回 Excel 文件
- html - CSS 排版问题(左对齐)
- flutter - how to get only day number from date in flutter
- amazon-web-services - AWS Athena - 从 Lambda 连接
- spring - @RequestBody 在集成测试范围内仅包含带有 kotlin 数据类的空值
- python - 如何更新绘图并保留 UI 设置?
- react-native - React Native:博览会开始导致抛出错误:找不到模块