gpt4 book ai didi

javascript - 在我的 Angular Controller 中调试 promise ,如果错误消息中没有文件名和 lineno,则非常困难

转载 作者:行者123 更新时间:2023-12-03 04:19:47 31 4
gpt4 key购买 nike

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

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

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是第一个console.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 不应该帮助它调试,它的唯一目的是记录消息。

有多个 console methods输出调用堆栈,即errorwarntrace。根据上下文可以使用其中之一。

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

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

$exceptionHandler(error)

The default implementation simply delegates to $log.error which logs it into the browser console.

并且 $log 仅使用 console:

Default implementation safely writes the message into the browser's console (if present).

ES6 Promise 实现为未捕获的 Promise 提供了默认处理程序(这种行为对于某些现代浏览器以及 core-js polyfill 来说都是如此)。为了调试的目的,可以在 catch 中重新抛出错误并导致错误:

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

关于javascript - 在我的 Angular Controller 中调试 promise ,如果错误消息中没有文件名和 lineno,则非常困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000607/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com