Angular/Information
[Angular] all error catch class 만들기
y0ngha
2022. 2. 6. 12:14
ErrorHandler Class를 구현(Implements)하면 된다.
ErrorHandler Class의 구조는 아래와 같다.
class ErrorHandler {
handleError(error: any): void
}
위 Class를 구현하면 모든 오류에 대해서 가져올 수 있다.
class MyErrorHandler implements ErrorHandler {
handleError(error) {
// do something with the exception
}
}
@NgModule({
providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}
모든 기능에서는 오류가 발생했을 때 try... catch 문법으로 오류 처리를 하고 있을텐데, catch 부분에서 특정 Error Class를 구현한 Class를 다시 throw 하고, 위 MyErrorHandler Class에서 오류에 대해 사용자에게 표시해주면 좋을 것 같다.