0
this snippet is from app.module.ts. I just created an angular 9 application 
and tried to implement ErrorHandler class to catch error but it is showing
the error at run time and page does not load.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ErrorHandler } from '@angular/core';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
    {provide: ErrorHandler, useClass: MyErrorHandler}
  ],
  bootstrap: [AppComponent]
})

class MyErrorHandler implements ErrorHandler {
  handleError(error) {
    alert(error);
    // do something with the exception
  }
}
export class AppModule { }

Exception in browser:

ASSERTION ERROR: Type passed in is not NgModuleType, it does not have 'ɵmod' property.
    at throwError (core.js:1344)
    at assertNgModuleType (core.js:2951)
    at compileNgModuleFactory__POST_R3__ (core.js:42275)
    at PlatformRef.bootstrapModule (core.js:42663)
    at Module../src/main.ts (main.ts:11)
    at __webpack_require__ (bootstrap:79)
    at Object.0 (main.ts:12)
    at __webpack_require__ (bootstrap:79)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
2
  • 1
    move your MyErrorHandler class to a separate file and import it in your AppModule and it should work. Commented May 26, 2020 at 21:27
  • It worked great. May I know the reason? Commented May 28, 2020 at 20:12

1 Answer 1

1

move your MyErrorHandler class to a separate file and import it in your AppModule and it should work.
Angular uses typescript decorators, which are defined above each class they belong to. In your case you were adding a decorator @NgModule to a class MyErrorHandler, that way Angular could not find proper configuration for main AppModule, and that's why you receive such error when compiling.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your great help and guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.