Application becomes unresponsive after encountering an exception

前端 未结 4 1832
星月不相逢
星月不相逢 2020-11-28 09:24

How to tell Angular 2 to not block the whole application when it encounters an exception?

I\'m not sure if it\'s even possible, because Google didn\'t enlighten me o

4条回答
  •  广开言路
    2020-11-28 10:07

    I tried to implement Custom Exception Handler as directed by @GünterZöchbauer

    and this worked for me


    Bootstrap your application with CustomExceptionHandler.

    import {ExceptionHandler} from 'angular2/core';
    
    class _ArrayLogger {
      res = [];
    
      log(s:any):void {
        this.res.push(s);
      }
      logError(s:any):void {
        this.res.push(s);
      }
      logGroup(s:any):void {
        this.res.push(s);
      }
      logGroupEnd() {
        if(this.res.length) {
    
          //this section executes if any error is logged by angular. 
    
          //do your stuff here
    
          /*e.g
    
            if(location.pathname !== 'dashboard') {
               window.location.href = '/dashboard'; // condition is required to prevent infinite loop
            }
    
            */
         }
      };
    }
    
    export class CustomExceptionHandler extends ExceptionHandler {
      constructor() {
        super(new _ArrayLogger(), true);
      }
    
      call(error, stackTrace = null, reason = null) {
        super.call(error, stackTrace, reason);
      }
    }
    
    bootstrap(MyApp, [provide(ExceptionHandler, {useClass: CustomExceptionHandler})])
    

提交回复
热议问题