使用 Angular 2 和 rjxs 检查端口上的超时错误
我想在向端口发送请求时检查请求是否超时。我只想在请求超时时打印错误。
myService.service.ts
checkStatus() : Observable<any> {
return this.http.get<any>('http://127.0.0.1:3001'); // simple example
}
myComponent.ts
this.myService.checkStatus.subscribe(() => {},
(err) => {
// if the timeout error is print error
}
}
我只想在修复超时错误时从屏幕上删除错误。如果返回错误 404 或其他错误,我不希望执行该操作并将其打印到屏幕上。
简而言之,如下所示,
- 将向端口发送请求并检查是否有超时错误。
- 如果存在超时错误,则会在屏幕上打印错误。(404或不同的错误不会打印,仅打印超时错误)
- 如果存在超时错误,将检查错误是否仍然存在,如果错误继续存在如果没有发生,屏幕上的文本将被删除。
I want to check if the request is timeout when I send a request to the port. I just want to print an error when the request is timeout.
myService.service.ts
checkStatus() : Observable<any> {
return this.http.get<any>('http://127.0.0.1:3001'); // simple example
}
myComponent.ts
this.myService.checkStatus.subscribe(() => {},
(err) => {
// if the timeout error is print error
}
}
I just want the error to be removed from the screen when the timeout error is fixed. if error 404 or a different error returns, I do not want the operation to be performed and printed to screen.
in short, it is as follows
- A request will be sent to the port and checked for a timeout error.
- If there is a timeout error, an error will be printed on the screen.(404 or different errors will not print, only timeout error)
- if there is a timeout error, it will be checked if the error persists, and if the error does not occur, the text on the screen will be removed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
retryWhen
以一定的时间间隔检查超时错误,这里是一个内部可观察值,外部可观察值仅用于为 uimyService.service.ts
myComponent 发出消息.ts
Using
retryWhen
to check timeOut error with certain interval,and which is here a inner observable,and outer observable only to emit mesages for uimyService.service.ts
myComponent.ts
你可以做这样的事情来捕获你的错误。
You can do something like this, to catch your error.