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

js 的 try catch 可以重写吗?

  •  
  •   MuscleOf2016 · 2019-08-01 08:47:19 +08:00 · 4947 次点击
    这是一个创建于 1701 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需要在项目所有 trycatch 中加个日志收集。

    19 条回复    2019-08-02 12:39:18 +08:00
    newghost
        1
    newghost  
       2019-08-01 08:55:22 +08:00
    js 是一般不写 try/catch,一般采用 error first,error 永远是回调的第一个参数,有 error 就用 console.log/ console.error 输出,只要把 console 重写到文件里就行。
    MuscleOf2016
        2
    MuscleOf2016  
    OP
       2019-08-01 09:00:06 +08:00
    @newghost 是的,不过像我现在遇到的情况有什么好的解决办法尼,代码已经没办法改了,准备用工具去遍历注入的。用正则?
    whileFalse
        3
    whileFalse  
       2019-08-01 09:01:21 +08:00
    不知道 Error 类能不能加私货。
    cctv1005s927
        4
    cctv1005s927  
       2019-08-01 09:01:40 +08:00
    @newghost 不一定吧..从 async / await 出来之后.. try catch 就用得很多了..
    Xxss
        5
    Xxss  
       2019-08-01 09:15:35 +08:00 via Android
    try {
    // ...
    } catch (e) {
    // ...
    } finally {
    // record log here
    }

    试试加一个 finally 块?
    maichael
        6
    maichael  
       2019-08-01 09:16:50 +08:00
    如果不换 try-catch 的写法的话,没办法。

    我的做法是所有 catch 都有一个 console.error,然后重写 console.error。
    lymanliu
        7
    lymanliu  
       2019-08-01 09:18:25 +08:00
    写个 webpack 插件,编译的时候注入,就好了吧
    sunzongzheng
        8
    sunzongzheng  
       2019-08-01 09:43:43 +08:00
    browser: window.onerror = function(message, source, lineno, colno, error) { ... }
    nodejs: process.on('uncaughtException', function(err) {
    console.error('Error caught in uncaughtException event:', err);
    })
    MuscleOf2016
        9
    MuscleOf2016  
    OP
       2019-08-01 09:45:21 +08:00
    @sunzongzheng catch 的异常都处理过了,会走 onerror?
    sunzongzheng
        10
    sunzongzheng  
       2019-08-01 09:52:32 +08:00
    @MuscleOf2016 #9 不会
    imlinhanchao
        11
    imlinhanchao  
       2019-08-01 09:55:32 +08:00   ❤️ 1
    code.replace(/(\}\s*catch\(\s*(\w+)\s*\)\s*{)/mg, "$1\nlogfuntion($2);\n");
    Torpedo
        12
    Torpedo  
       2019-08-01 10:01:47 +08:00
    你需要一个 babel 插件
    MuscleOf2016
        13
    MuscleOf2016  
    OP
       2019-08-01 10:02:56 +08:00
    @Torpedo 哪个有推荐不
    Torpedo
        14
    Torpedo  
       2019-08-01 10:04:29 +08:00
    @newghost 怎么就不写 try catch 了? err first 只是一种风格,用 async await 不就要写 try catch 了?
    而且本身程序必然有错误捕获的模块,难道不用 try catch ?
    Torpedo
        15
    Torpedo  
       2019-08-01 10:13:43 +08:00   ❤️ 1
    @MuscleOf2016 搜下,或者自己写个。https://github.com/darkyndy/babel-plugin-auto-logger
    我觉得你还是代码里分哪些是异常,哪些是错误 。设计下结构。统一捕获一下
    otakustay
        16
    otakustay  
       2019-08-01 10:16:32 +08:00   ❤️ 1
    @newghost first error 是 callback 的模式,同步不用 callback 肯定是 try/catch,异步 js 玩 Promise 多好多年了也还是 try/catch ……

    要重写是不可能的,上 babel 搞定还是比较容易的
    nigelvon
        17
    nigelvon  
       2019-08-01 10:17:56 +08:00   ❤️ 1
    整个项目查找替换一下,catch 里面执行上报的方法,也就 1 分钟的事儿。
    mystorp
        18
    mystorp  
       2019-08-01 10:33:34 +08:00 via Android   ❤️ 1
    jscodeshift ?原理就是遍历 ast,查找 try catch。并加上自己的 log 代码。楼上说的 babel 插件也是类似的原理。不过,我觉得 17 楼的回答也很有意义,看怎么选择了
    jifengg
        19
    jifengg  
       2019-08-02 12:39:18 +08:00 via Android
    catch 中是用什么记录异常的,如果是 console.error,那就重写它。评估一下不要对现有系统有影响就行。

    可别说 catch 里面连打印异常都没有。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2713 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:21 · PVG 08:21 · LAX 17:21 · JFK 20:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.