【问题标题】:How can I disable error logging in Promise reject?如何在 Promise 拒绝中禁用错误​​日志记录?
【发布时间】:2020-04-24 13:32:15
【问题描述】:

我正在使用 HttpClient 和 promises。 我使用 httpclient.get 从后端接收数据,一切正常,但我遇到的错误有问题。事实上,我需要这些错误以供以后详细说明,所以我也重新运行它们,但我不希望将响应记录为错误日志。

return new Promise<any>((resolve, reject) => {
            this.httpClient.get(url, {"some parametres"}).subscribe(
                response => {
                    resolve(response.body);
                },
                error => {
                    reject(error);
                }
            )
        });

如您所见,我只想转发错误数据。此脚本中的任何内容都可以正常工作。唯一的问题是,在我的 html 页面中,控制台因拒绝以下表单而出现错误消息:

 Error: Uncaught (in promise): HttpErrorResponse: {"some error message"}
    {"some Traceback"}

有没有办法处理错误或禁用日志记录,这样我就不会得到所有的错误日志?

谢谢!

【问题讨论】:

    标签: typescript angular-promise angular-httpclient angular-http error-logging


    【解决方案1】:

    也许是这样:

    function myPromise(parms) { //create a function who return a Promise
        return new Promise<any>((resolve, reject) => {
                    this.httpClient.get(url, {"some parametres"}).subscribe(
                        response => {
                            resolve(response.body);
                        },
                        error => {
                            reject(error);
                        }
                    )
                });
        }
    
        myPromise(args) //then, call that function and use "then" and "catch"
           .then((success)=> {
             console.log(success); //when success 
           })
           .catch((error) => {
             console.log(error) //when error
           });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多