推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
MuscleOf2016
0D

js 的 try catch 可以重写吗?

  •  
  •   MuscleOf2016 · Aug 1, 2019 · 6734 views
    This topic created in 2654 days ago, the information mentioned may be changed or developed.

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

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

    试试加一个 finally 块?
    maichael
    maichael
    Aug 1, 2019
    6
    如果不换 try-catch 的写法的话,没办法。

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

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

    可别说 catch 里面连打印异常都没有。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4736 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 08:30 · PVG 16:30 · LAX 00:30 · JFK 03:30
    ♥ Do have faith in what you're doing.