如果图书馆吞没所有的异常,我如何调试我的异步的,承诺的代码? [英] How do I debug my asynchronous, promise based code if the library is swallowing all the exceptions?

查看:137
本文介绍了如果图书馆吞没所有的异常,我如何调试我的异步的,承诺的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



JSFiddle
http://jsfiddle.net/missingno/Gz8Pe/2/



我有一些代码如下:

  var d = new Deferred(); 
d.resolve(17);
return d.then(function(){
//做一些东西...
})
.then(function(){
var obj = a_funtion_that_returns_null_on_IE ();
var x = obj.some_property; // BOOM!
});

问题是,当我在IE上,我可以看到是 obj'为null或不是对象错误,没有引用相应的行号,没有调试器在违规行中停止(就像我希望的那样)。 / p>

这种问题使得代码变得难以调试,现在我可以想到的唯一的解决方案(混乱控制流程库或借助逐步 - 调试器或console.log的步骤调试)是我不想做的事情。



我认为是在



为了在链条被触发之后添加errbacks,然后将抢先捕获回调引发的任何异常。我认为这是因为IE调试器不会停止错误或显示常见的错误消息,其中的行号。



没有行号的错误消息来自控制流程库:它提供了一个 deferredOnError 钩子,每当异常被捕获并保存以供以后使用时,调用它,默认行为是console.error-ing错误对象:

  dojo.config.deferredOnError = function(err){
//有机会记录异常之后它被then
//捕获,或用其他东西与
console.error(err);
}

可惜的是,我找不到一种方式来获取行号或堆栈跟踪从IE中的错误对象和钩子调用的方式,不允许我只是重新抛出异常,让它冒泡到高峰。



什么我想要



我想有一个更好的方法来调试异步代码,然后一起跟踪调试器。在最好的情况下,使调试器停止异常(如在未处理的异常情况下)的一种方法至少有一种方式来获取行号或堆栈

解决方案

这个工作与任何框架没有事先配置和所有最近的浏览器支持这个。



暂停捕获异常: 这实际上可以阻止JavaScript的执行,并且会随着正在发生的事情而将问题代码的确切位置。





在Chrome中:


  1. 开发人员工具

  2. 来源标签,

  3. 暂停异常(sto p-like图标),然后

  4. 暂停捕获异常复选框


The Problem

JSFiddle: http://jsfiddle.net/missingno/Gz8Pe/2/

I have some code that looks like this:

var d = new Deferred();
d.resolve(17);
return d.then(function(){
     //do some stuff...
})
.then(function(){
    var obj = a_funtion_that_returns_null_on_IE();
    var x = obj.some_property; //BOOM!
});

The problem is that when I am on IE all I can see are 'obj' is null or not an object errors, without any reference to the corresponding line number and without the debugger halting at the offending line (like I wish it would).

This kind of issue is making the code a pain to debug and the only solutions I can think of right now (messing around with the control flow library or resorting to step-by-step debugging with the debugger or console.log) are things I would rather not have to do.

What I think is going on

In order to allow errbacks to be added after the chain is fired, then will preemptively catch any exceptions thrown by the callbacks. I think this is the reason for the IE debugger not halting on the error or showing the usual error message witht the line number in it.

The error messages without the line numbers are coming from the control-flow library: it provides a deferredOnError hook that is called whenever an exception is caught and saved for later and the default behaviour is console.error-ing the Error object:

dojo.config.deferredOnError = function(err){
    //a chance to log the exception after it is captured by "then"
    //or do other things with it
    console.error(err);
}

Sadly, I could not figure out a way to get the line number or stack trace from the error object in IE and the hook is called in a way that does not allow me to just rethrow the exception and let it bubble up to the toplevel.

What I want

I want to have a better way to debug the async code then goind along with the debugger step-by-step. In the best case a way to have the debugger halt on the exceptions (as it does on unhandled exceptions) or at least a way to get line numbers or stack traces from the Error object that was thrown.

解决方案

This works with any framework without prior configuration and all recent browsers support this.

Pause On Caught Exceptions: This will actually stop javascript's execution and will take you exactly where the problematic code is, as it's happening.

In Chrome:

  1. Developer Tools,
  2. Sources tab,
  3. Pause on exceptions (stop-like icon) and then
  4. the Pause On Caught Exceptions checkbox

这篇关于如果图书馆吞没所有的异常,我如何调试我的异步的,承诺的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆