且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取$ HTTP调用的全部调用堆栈跟踪

更新时间:2021-12-31 04:37:15

所以,你看,这个问题主要是因为角度的$ HTTP很烂。我们对此深感抱歉。

So, you see, this issue is mostly because angular's $http sucks. Sorry about that.

让我们尝试使用蓝鸟库,因为它提供了很长的堆栈跟踪。

Let's try to use the bluebird library, because it provides long stack traces.

Promise.longStackTraces();
Promise.resolve($http.get('...'));

您得到下面的堆栈跟踪:

You get the following stack trace:

Possibly unhandled Error: [object Object]
    at Promise$_rejectFromThenable (http://cdn.jsdelivr.net/bluebird/1.2.4/bluebird.js:4736:52)
    at wrappedErrback (https://code.angularjs.org/1.3.0-beta.5/angular.js:11334:78)
    at wrappedErrback (https://code.angularjs.org/1.3.0-beta.5/angular.js:11334:78)
    at wrappedErrback (https://code.angularjs.org/1.3.0-beta.5/angular.js:11334:78)
    at https://code.angularjs.org/1.3.0-beta.5/angular.js:11467:76
    at Scope.$eval (https://code.angularjs.org/1.3.0-beta.5/angular.js:12418:28)
    at Scope.$digest (https://code.angularjs.org/1.3.0-beta.5/angular.js:12230:31)
    at Scope.$apply (https://code.angularjs.org/1.3.0-beta.5/angular.js:12522:24)
    at done (https://code.angularjs.org/1.3.0-beta.5/angular.js:8207:45)
    at completeRequest (https://code.angularjs.org/1.3.0-beta.5/angular.js:8412:7) 

Plunker这里。)

最重要的线是第一条:可能未处理的错误:[对象的对象]

The most important line is the first: Possibly unhandled Error: [object Object].

是的。一个对象被抛出,而不是一个真正的错误对象,用堆栈属性附加到它。对于参考,这里是如何抛出一个错误,并保持它的栈与它一起:https://github.com/Ralt/newerror/blob/master/index.js

Yep. An object is thrown, not a real Error object, with the stack property attached to it. For the reference, here is how to throw an error and keep its stack along with it: https://github.com/Ralt/newerror/blob/master/index.js

那么,如何解决这一问题?这取决于以下几个决定:

So, how to fix this? It depends on several decisions:

  • 请你想添加一个适当的承诺lib中,使长的堆栈跟踪?
  • 请您要使用另一个XHR的lib抛出纠正错误?

蓝鸟。 AFAIK,它是提供长期的堆栈跟踪的几个之一,它是最快的国家之一在那里。

If you want to add a real Promise lib, use bluebird. AFAIK, it is one of the few that provides long stack traces, and it is the fastest one out there.

有关适当XHR LIB抛出真正的错误,我怕你的运气那里。编写一个自定义的与您希望浏览器的支持是不是真的很难,但。由于没有IE8的支持,这工作(使用蓝鸟):

For a proper xhr lib that throws real errors, I'm afraid you're out of luck there. Writing a custom one with the support for browsers you want isn't really hard though. With no IE8 support, this works (with bluebird):

function xhr(url) {
    return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
            resolve(xhr.responseText);
        };
        xhr.onerror = reject;
        xhr.open('GET', url);
        xhr.send();
    });
}

Plunker这里。)

正如你所看到的堆栈跟踪信息:

As you can see, the stack trace is informative:

获取$ HTTP调用的全部调用堆栈跟踪