Я видел несколько решений для обработки ошибок.
Основная идея лежит в основе этого объяснения: https://angular.io/api/core/ErrorHandler
Я пытался реализовать его, чтобы перехватить TypeError, но он не работает:
@Injectable()
export class ErrorsHandler implements ErrorHandler {
constructor(
private injector: Injector,
) { }
handleError(error: Error | HttpErrorResponse | TypeError) {
const notificationService = this.injector.get(NotificationService);
const dataService = this.injector.get(DataService);
const router = this.injector.get(Router);
if (error instanceof HttpErrorResponse) {
if (!navigator.onLine) {
// No Internet connection
return notificationService.notify('No Internet Connection');
}
// Http Error
// Send the error to the server
console.log(error);
// Show notification to the user
return notificationService.notify(`${error.status} - ${error.message}`);
} else if (error instanceof TypeError) {
console.log('This is TypeError!', error);
}
}
}