【发布时间】:2017-04-24 15:04:50
【问题描述】:
我想捕获所有 http 错误以通知用户发生的问题。
所以,我创建了一个通过 catch 执行 http get 的方法。
get(path: string): Promise<any> {
let headers = new Headers();
return this.http.get(path, { headers: headers })
.toPromise()
.then(response => { ... })
.catch((response: Response) => { this.errorHandler(response); });
}
当路径参数为 '/fault-url' 时,捕获按预期处理。
但是当路径是 'http://localhost:5000/fault-url' 时,catch 方法没有捕获到错误。 我已将日志记录添加到所有不同的地方,但它使 get 方法中止。 控制台日志显示以下错误
选项http://localhost:5000/fault-url net::ERR_CONNECTION_REFUSED
异常:响应状态为:0,URL 为空
未捕获的响应 {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers...}
http 真的中止了还是我做错了什么?
【问题讨论】:
标签: angular typescript http try-catch