Angular2 中的错误处理 [英] Error handling in Angular2

查看:65
本文介绍了Angular2 中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Angular 2 服务,该服务从一个安静的 WebApi 后端返回数据.

I'm working on an Angular 2 service returning data from a restful WebApi backend.

我试图让它优雅地处理不可用的服务,但是我在错误响应中收到了意外信息.

I'm trying to make it gracefully handle the service not being available however I'm getting unexpected information in the error responses.

这是代码

update(fund, transactionId:string, showToastOnError: boolean, useCorrectUrl: boolean) {
        let options = this.getStartCall();
        options.headers.append("transaction-id", transactionId)
        let url = fundsUrl + (useCorrectUrl ? "" : "breakTheUrl");
        return this._http.put(url, JSON.stringify(fund), options)
            .map(res => res.json())
            //.retry(5)
            .catch(errorResponse => {
                let res = <Response>errorResponse;
                let err = res.json();
                let emsg = err ?
                    (err.error ? err.error : JSON.stringify(err)) :
                    (res.statusText || 'unknown error');
                this._logger.log(emsg, "The fund was not updated", LogLevel.Error, showToastOnError);
                return Observable.throw(emsg);
            })
            .finally(() => this._spinnerService.hide());

    }

当我查看网络流量时,我看到了预期的 404 错误.

When I look at the network traffic I see the 404 error as expected.

我的问题在于我的 catch 函数.

My problem is in my catch function.

这是我看到的价值:

JSON.stringify(errorResponse)

{"_body":{"isTrusted":true},"status":200,"statusText":"Ok","headers":{},"type":3,"url":null}"

errorResponse.json() 

bubbles: false
cancelBubble: false
cancelable: false
currentTarget: XMLHttpRequest
defaultPrevented: false
eventPhase: 2
isTrusted: true
isTrusted: true
lengthComputable: false
loaded: 0
path: Array[0]
position: 0
returnValue: true
srcElement: XMLHttpRequest
target: XMLHttpRequest
timeStamp: 1460990458693
total: 0
totalSize: 0
type: "error"
__proto__: XMLHttpRequestProgressEvent

我在这里做错了什么还是这是 Angular2 beta 15 中的错误?

Am I doing something wrong here or is this a bug in Angular2 beta 15?

推荐答案

大多数情况下,当在底层 XHR 对象上调用 onerror 回调时,您会有这样的错误值.在源代码中看到这一行:https://github.com/angular/angular/blob/master/modules/angular2/src/http/backends/xhr_backend.ts#L70.

Most of time, you have such a value for an error when the onerror callback is called on the underlying XHR object. See this line in the source code: https://github.com/angular/angular/blob/master/modules/angular2/src/http/backends/xhr_backend.ts#L70.

也就是说,它不应该发生在 404 状态代码上,而应该发生在 net::ERR_NAME_NOT_RESOLVED 之类的错误上.

That said, it shouldn't occur for a 404 status code but rather on an error like net::ERR_NAME_NOT_RESOLVED.

404 状态代码由 onload 回调处理:https://github.com/angular/angular/blob/master/modules/angular2/src/http/backends/xhr_backend.ts#L37.

404 status code is handled by the onload callback: https://github.com/angular/angular/blob/master/modules/angular2/src/http/backends/xhr_backend.ts#L37.

我使用 beta15 进行了测试,但无法重现您的问题.

I made a test with the beta15 and I can't reproduce your problem.

查看此 plunkr:https://plnkr.co/edit/SB3KLZlbJT3wm9ATAE0R?p=preview.

See this plunkr: https://plnkr.co/edit/SB3KLZlbJT3wm9ATAE0R?p=preview.

这篇关于Angular2 中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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