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);
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; }
source share