如何停止浏览器控制台中zone.js生成的错误? [英] How to stop errors generated by zone.js in browser console?

查看:217
本文介绍了如何停止浏览器控制台中zone.js生成的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在httpService.ts中的代码

public HttpPost(url: string, data: any): Observable<Response> 
{
    let headers = new Headers();
    let reqOpts = new RequestOptions();
    headers.append(AUTH_CONTENT_TYPE_KEY, AUTH_CONTENT_TYPE);
    if (this.Token) {
        headers.append(AUTH_HEADER_KEY, AUTH_TOKEN_PREFIX + this.Token);
    }
    reqOpts.headers = headers;
    return this.http.post(url, data, reqOpts).timeout(30000).map((res: Response) => res)
        .catch((err: Response) => { return Observable.throw(err) });
}

如果我尝试使用此服务以这种方式调用API:

this.httpService.HttpPost(url, data).subscribe(
    (res: any) => 
    {
       // do something
    },
    (error: any) => 
    {
        if (error.status === 409)
            // do something
        else if (error.status === 401)
            // do something
        else
           // do something
    }
);

,如果res状态代码不是200,例如401、403或409,则这些烦人的错误是由console.js中的zone.js生成的. (对不起,API的隐藏部分网址)

所以无论如何,都可以停止zone.js在控制台中生成这种烦人的错误,尝试用Google搜索示例示例的解决方案,但尚未找到,谢谢!

试图在main.ts中使用它,但不起作用:

window.console.error = function() {};

添加堆栈跟踪:

解决方案

当网络请求失败时,Chrome会在控制台中显示错误.该错误会显示失败的网址,收到的HTTP状态以及请求的来源.通常,在Angular中,您是在响应用户的某些操作而发出HTTP请求. Angular使用区域捕获这些动作,因此,几乎所有错误的根源都在zone.js中.但是,如果检查整个堆栈跟踪,您将看到在调用您自己的代码后发生错误.只是zone.js中的代码最终调用了该代码.

因此,在控制台中不看到请求错误的唯一方法是将chrome控制台配置为忽略它们,但我认为没有编程的方式.

我确定您正在考虑window.onerror,它在理论上捕获了任何未捕获的异常.但是这些网络错误不是JavaScript异常,因此您将无法捕获它们.

我不会替换console.error.因为如果这样做,您将看不到代码中可能存在的真正错误.无论如何,如上所述,在这种情况下,是Chrome正在向控制台写入内容,而隐藏这些错误的唯一方法就是过滤它们(如我所言,但这当然只对您有用).

Here is my code in the httpService.ts

public HttpPost(url: string, data: any): Observable<Response> 
{
    let headers = new Headers();
    let reqOpts = new RequestOptions();
    headers.append(AUTH_CONTENT_TYPE_KEY, AUTH_CONTENT_TYPE);
    if (this.Token) {
        headers.append(AUTH_HEADER_KEY, AUTH_TOKEN_PREFIX + this.Token);
    }
    reqOpts.headers = headers;
    return this.http.post(url, data, reqOpts).timeout(30000).map((res: Response) => res)
        .catch((err: Response) => { return Observable.throw(err) });
}

If I try to using this service to call an API like this way:

this.httpService.HttpPost(url, data).subscribe(
    (res: any) => 
    {
       // do something
    },
    (error: any) => 
    {
        if (error.status === 409)
            // do something
        else if (error.status === 401)
            // do something
        else
           // do something
    }
);

and if the res status code is one other than 200, such as 401, 403 or 409, then these kind of annoying errors are generated by zone.js in console. (sorry for hide part url of APIs)

So is there anyway to stop zone.js to generate such a kind of annoying errors in console, tried to google for a solution with example but not found yet, Thanks!

tried to use this in main.ts but not worked:

window.console.error = function() {};

Edit: add stack trace:

解决方案

When a network request fails, Chrome shows the error in the console. The error displays the url that failed, the HTTP status received and the origin of the request. Usually, in Angular, you're making an HTTP request in response to some action from the user. Angular captures those actions using zones, so, almost any error has its origin in zone.js. If you check the whole stack trace, however, you'll see that the error happens after calls to your own code. It's just that is code in zone.js what ends up calling that code.

So, the only way not to see request errors in the console, is to configure your chrome console to ignore them, but I don't think there is a programmatic way of doing so.

I'm sure you're thinking about window.onerror, which in theory captures any uncaught exception. But those network errors are not JavaScript exceptions, so you won't be able to capture them.

I'd wouldn't replace console.error. because if you do, you won't be able to see true errors you might have in your code. Anyway, as stated, in this case is Chrome who is writing to the console, and the only way to hide those errors is, as I already said, filtering them (but it'll only work for you, of course).

这篇关于如何停止浏览器控制台中zone.js生成的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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