How to "expand" ZoneAwareError?

In my Angular 2 application, I have a solution advocate that tries to execute xhr and throws a custom error if it fails:

return this.service.getProduct (id) .catch (err => Observable.throw(new MyError(err.message, route, 500, err))); 

MyError is simply an extension of Error :

 export class MyError extends Error { constructor(message, routeSnapshot, code, err) { super(message); this.name = 'MyError'; this.routeSnapshot = routeSnapshot; this.code = code; this.err = err; } } 

My global error handler gets an instance of ZoneAwareError instead of MyError :

 export class MyErrorHandler implements ErrorHandler { handleError(error) { console.log('handling err', error); // this prints a ZoneAwareError on the console } } 

Is it possible for Error be wrapped in a ZoneAwareError ? How to expand it to get MyError back? Can I rely on the error.rejection property? (Because I see MyError there).

=== Edit:

Well, I just found that Angular wraps errors in different subtypes of Error , not just ZoneAwareError . So far I have this function to deploy them, but it is a bit hacked.

 function unwrap (err) { let res = err.rejection ? err.rejection : err; while (res.originalError) { res = res.originalError; } res.name = res.name || 'unknown'; return res; } 
+5
source share
1 answer

You can see how the deployment is done in Angular itself: angular/modules/@angular/core/src/error_handler.ts , however they don't seem to handle rejection (which is very useful) in any way, so before unpacking I use the following:

  if(error.hasOwnProperty('rejection')) { error = error.rejection; } 

UncaughtPromiseError interface in zone.js.

+4
source

Source: https://habr.com/ru/post/1263789/


All Articles