在我的角度控制器中调试承诺,在错误消息中没有文件名和行号非常困难



我包含了一堆函数,其中一个函数有一个错误,但是哪一个在哪里? 我怎么能看到呢?

这是浏览器报告错误的地方:

Promise.all([findProperties])
    .then(makeReport)
    .then(showReport)
    .catch(err => {
        console.log("ERROR: " + err.message );
        console.log("ERROR: " + err.file );
        console.log("ERROR: " + err.lines );
        throw new Error('Higher-level error. ' + err);
    })
    .catch(err => {
        console.log("reportAgentSaleController ERROR: " + err);
    })

像 63 是第一个控制台.log

index.js:63 ERROR: Cannot read property 'replace' of undefined
index.js:64 ERROR: undefined
index.js:65 ERROR: undefined
index.js:69 ERROR: Error: Higher-level error. TypeError: Cannot read property 'replace' of undefined

当我得到 9 包含数百行充满替换命令的代码时,真的很难找到该错误。

如何找出出错的位置和文件?

编辑 - 尝试拒绝

下面是一个示例。
控制台日志将像这样返回

here comes the error (line 114)
err: ReferenceError: conXXXsole is not defined (line 102)

错误实际上在第 115 行,那么我如何让它告诉我它在第 115 行?

function testErr(){
    var first = new Promise(
        (resolve, reject) => {
        if (1==1) {
            resolve();
        } else 
            reject();
    });

    var second = function(){
        var test=funcWithErr('aaa');
    }
    Promise.all([first])
        .then(second)
        .catch(err => { console.log('err: ' + err) }
)}

function funcWithErr(text){
    console.log ('here comes the error')  // line 114
    conXXXsole.log ('this is the error')  // line 115
    return text;
}
console.log不应该

帮助它调试,它的唯一目的是记录一条消息。

有许多

控制台方法可以输出调用堆栈,即errorwarntrace。根据上下文,可以使用其中之一。

如果是严重错误,则可以console.error(error),如果是预期错误,则可以console.warn(error)

AngularJS中的惯用方式是使用$exceptionHandler服务:

$exceptionHandler(error)

默认实现只是委托给 $log.error,后者将其记录到浏览器控制台中。

$log只使用console

默认实现安全地将消息写入浏览器的控制台(如果存在(。

ES6 promise 实现为未捕获的承诺提供了默认处理程序(此行为适用于某些现代浏览器,并且core-js polyfill 也是如此(。为了调试,错误可以重新抛出catch并导致错误:

.catch(err => {
    console.log("reportAgentSaleController ERROR: " + err);
     throw err;
})

最新更新