【问题标题】:Error handling in Angular HttpClient (when not finding a record is not considered an error in the business rule)Angular HttpClient 中的错误处理(当没有找到记录时不被视为业务规则中的错误)
【发布时间】:2018-07-31 13:28:20
【问题描述】:

当我需要处理 Angular 服务中的错误时,我遇到了问题。在我的场景中,404 返回对我来说不是错误,我需要将 Null 返回到我的前面。我的问题是我的控制台GET http://localhost:9000/api/funcionarios/user/3 404 (Not Found) 中仍然出现错误。每次我的应用程序发生错误时,都会触发我的服务生成日志(在这种情况下不应该发生)

component.ts

this.funcionario.findByUser(parseInt(account.id, 10)).subscribe((r) => {
    this.setFuncionario(r.body);
}, (err) => {
    this.setFuncionario(null);
});

service.ts

findByUser(id: number): Observable<EntityResponseType> {
    return this.http
        .get<Funcionario>(`${this.resourceUrlByUser}/${id}`, {
            observe: 'response'
        })
        .map((res: EntityResponseType) => res);
}

结果是预期的,但您正在生成这些错误日志。有什么想法吗?

谢谢

【问题讨论】:

  • 想知道为什么要从控制台隐藏错误?如果你在控制台打开时去谷歌搜索一些东西,你会被错误轰炸。事情就是这样。
  • @ChristianScillitoe 在我的情况下,不返回任何东西不是错误,而是预期的业务规则。所以我想我应该在后端处理这种情况,对吗?
  • @Quentin 感谢您的帮助。在您报告的链接中,问题显然是用户不想要错误消息。在我的情况下,空响应不是错误,而是我的应用程序预期的结果。我认为在这种情况下我应该在后端进行处理,对吗?

标签: angular


【解决方案1】:

就我而言,我有这个: 如果存在函数返回"funcionario: {&lt;object funcionario&gt;}",如果不存在则返回404

如果用户有 funcionario 做某事,我就在前面工作,否则我做一些不同的事情

我替换了我的代码搜索用户。如果用户有函数,则返回用户对象"user: {funcionario: &lt;object funcionario&gt;}",如果不存在则返回"user: {funcionario: null}"

感谢您的合作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多