Reputation: 393
How can I make my app resilient to exceptions.
Currently, each time there is an exception, the UI freezes and I have to restart the app.
Thanks.
Upvotes: 3
Views: 3289
Reputation: 4539
UPDATE: Latest angular2, there is ErrorHandler
not ExceptionHandler
in angular core, which is now @angular/core
not @angular2/core
.
import { ErrorHandler } from '@angular/core';
export class MyExceptionHandler implements ErrorHandler {
handleError(error) {
// do something with the error
}
}
Link to new angular class: https://angular.io/docs/ts/latest/api/core/index/ErrorHandler-class.html
Upvotes: 2
Reputation: 44659
You can use Angular 2 ExceptionHandler.
The default implementation of ExceptionHandler prints error messages to the Console. To intercept error handling, write a custom exception handler that replaces this default as appropriate for your app.
So you can create your own implementation of this class
class MyExceptionHandler implements ExceptionHandler {
call(error, stackTrace = null, reason = null) {
// do something with the exception
}
}
And then, include it in your top-most component (the App component) like this:
import {ExceptionHandler,provide} from '@angular2/core';
import {MyExceptionHandler} from './folder/file-name';
@Component({
providers: [provide(ExceptionHandler, {useClass: MyExceptionHandler})]
})
export class MyApp{...}
UPDATE
As of Ionic2 RC, in order to replace the ExceptionHandler
for own custom implementation, we nee to include it in the NgModule
like this:
@NgModule({
providers: [{provide: ErrorHandler, useClass: MyExceptionHandler}]
})
class MyModule {}
Upvotes: 1