ErrorHandler:与 promise 一起使用时无法获取错误对象

Vik*_*iya 3 angular angular5

我正在构建一个实现自定义错误处理程序的角度应用程序。

当你有一个自定义的错误处理程序并且当一个 observable 与 http 一起使用并且当错误没有被捕获时,使用一个 catch 块,如下所示

this.http.get('/doesntExist').subscribe();
Run Code Online (Sandbox Code Playgroud)

自定义 ErrorHandler 的 handleError() 函数获取一个 HttpErrorResponse 对象,该对象可用于获取必要的信息并全局处理错误。但是当你使用承诺时,就像下面这样

this.http.get('/doesntExist').toPromise();
Run Code Online (Sandbox Code Playgroud)

而不是 HttpErrorResponse ,将抛出一个字符串说

错误:未捕获(承诺):HttpErrorResponse:

并且您无法在 handleError() 方法中真正获得错误信息。你可以在这里看到它的实际效果

这是理想的行为吗?难道我做错了什么?有什么解决方法吗?请帮忙。我被卡住了;

更新

控制台中字符串错误的堆栈跟踪

Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":[],"lazyUpdate":null},"status":200,"statusText":"OK","url":"https://angular-promise-bug.stackblitz.io/doesntExist","ok":false,"name":"HttpErrorResponse","message":"Http failure during parsing for https://angular-promise-bug.stackblitz.io/doesntExist","error":{"error":{},"text":"<!DOCTYPE html>\n<html>\n<head>\n  <link href=\"https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700|Lato:400,700,900\" rel=\"stylesheet\">\n  <base href=\"/\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />\n  <link rel=\"stylesheet\" media=\"screen\" href=\"https://c.staticblitz.com/assets/preview-8222014a50f8588c56d057621cdaf871.css\" />\n  <script src=\"https://c.staticblitz.com/assets/common-bd7eff1ca58185bf8131b.js\" crossorigin=\"anonymous\"></script>\n  <script src=\"https://c.staticblitz.com/assets/ext-fb68da629568861fd08cb.js\" crossorigin=\"anonymous\"></script>\n  <script src=\"https://c.staticblitz.com/d/webcontainer.a966dcc4085d3fff5bb.js\" crossorigin=\"anonymous\"></script>\n  <script src=\"https://c.staticblitz.com/assets/preview-3a0c9433aa42f56dbd90b.js\" crossorigin=\"anonymous\"></script>\n  <script>(function(){_preboot(\"https://l.staticblitz.com/b/v1/angular-promise-bug/f9f42b37e9b\",{p:\"stackblitz\",a:\"AIzaSyBZSvuCzbUhuRrSps-HjM5bFClLPaFF9Vg\",o:true})})()</script>\n</head>\n<body></body>\n</html>\n"}}
    at resolvePromise (angular-promise-bug.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:814)
    at eval (angular-promise-bug.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:724)
    at SafeSubscriber.eval [as _error] (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Observable.js:99)
    at SafeSubscriber.__tryOrUnsub (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:209)
    at SafeSubscriber.error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:160)
    at Subscriber._error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:93)
    at Subscriber.error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:73)
    at MapSubscriber.Subscriber._error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:93)
    at MapSubscriber.Subscriber.error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:73)
    at FilterSubscriber.Subscriber._error (angular-promise-bug.stackblitz.io/turbo_modules/rxjs@6.3.3/internal/Subscriber.js:93)
Run Code Online (Sandbox Code Playgroud)

Ara*_*orn 6

error.rejection应该让你的HttpErrorResponse当您使用toPromise()。我在 stackblitz 中测试了以下内容

  handleError(error) {
    const zone = this.injector.get(NgZone);
    console.log('Here')
    console.log(error)

    if (!(error instanceof HttpErrorResponse)) {
      error = error.rejection;
    }
    console.log(error);
    alert('Open console to see the error')
  }
Run Code Online (Sandbox Code Playgroud)