V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
xiaoming1992
V2EX  ›  JavaScript

求助关于 js Error.stack 的问题

  •  
  •   xiaoming1992 · 2020-05-10 16:37:43 +08:00 · 1810 次点击
    这是一个创建于 1657 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我希望对Error.stack做进一步的处理(其实就是不显示namemessage那一行), 但是只要一处理, 就不能映射到sourceMap了, 代码和打印结果如下, can you help help me?

    const { stack } = new Error("")
    if (stack) {
      console.log(stack)
      console.log(stack.split("\n").splice(1).join("\n"))
    }
    
    // 这是 console.log(stack)
    // sourceMap 正确
    Error
        at log (index.ts:7)
        at withinRect (index.tsx:39)
        at index.tsx:202
        at updateMemo (react-dom.development.js:16835)
        at Object.useMemo (react-dom.development.js:17315)
        at useMemo (react.development.js:1643)
        at App (index.tsx:202)
        at renderWithHooks (react-dom.development.js:16241)
        at updateFunctionComponent (react-dom.development.js:18328)
        at beginWork$1 (react-dom.development.js:20151)
    
    
    // 这是 console.log(stack.split("\n").splice(1).join("\n"))
    // sourceMap 错误
        at log ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:66434:17)
        at withinRect ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:65917:59)
        at http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:66044:96
        at updateMemo ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:41208:19)
        at Object.useMemo ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:41688:16)
        at useMemo ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:54182:21)
        at App ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:66044:74)
        at renderWithHooks ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:40614:18)
        at updateFunctionComponent ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:42701:20)
        at beginWork$1 ( http://192.168.3.7:8080/static/scripts/index.e7695f.js?e7695f02d813f4d2c31a:44524:16)
    
    5 条回复    2020-05-22 12:02:10 +08:00
    YadongZhang
        1
    YadongZhang  
       2020-05-10 18:24:17 +08:00
    好像力扣前端笔试题?
    xiaoming1992
        2
    xiaoming1992  
    OP
       2020-05-10 20:05:55 +08:00   ❤️ 1
    @YadongZhang 刚刚试了一下,sourceMap 发生时机应该是在 console.log 之后,根据 console.log 打印出的字符串进行替换。并且 Error 的 name 应该必须是原生 Error 的 name,只要保留第一行的 Error name,后面的行可进行删减。
    Arrowing
        3
    Arrowing  
       2020-05-11 08:33:48 +08:00 via Android
    没有过这样的需求,先插眼
    zqaq520
        4
    zqaq520  
       2020-05-21 10:21:45 +08:00
    同问啊,我最近在做前端错误监控系统也遇到类似问题,怎么破?
    xiaoming1992
        5
    xiaoming1992  
    OP
       2020-05-22 12:02:10 +08:00 via Android
    @zqaq520 按照我 2 楼说的做就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1427 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:23 · PVG 01:23 · LAX 09:23 · JFK 12:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.