Skip to content

NgModuleFactoryLoader: load method modifies Errors type? #21964

@maxime1992

Description

@maxime1992

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

With a simple global error handler:

export class MyErrorHandler extends ErrorHandler {
  handleError(error) {
    if (error instanceof CustomError) {
      console.log('Caught a CustomError!');
    } else {
      console.log('Caught a "normal" error');
    }
  }
}

I want to do a special action when an instance of CustomError is thrown.

That error can only come from a custom module loader:

export class ModuleLoader implements NgModuleFactoryLoader {
  private systemJsNgModuleLoader: SystemJsNgModuleLoader;

  constructor(
    private readonly compiler: Compiler,
    @Optional() config?: SystemJsNgModuleLoaderConfig
  ) {
    this.systemJsNgModuleLoader = new SystemJsNgModuleLoader(compiler, config);
  }

  public load(path: string) {
    if (path.includes('lazy-loaded')) {
      throw new CustomError('!!custom error!!');
    } else {
      return this.systemJsNgModuleLoader.load(path);
    }
  }
}

(simplified for demo here): Just throwing on a given path.

And in my console it displays:

Caught a "normal" error

Expected behavior

I would expect in console to see:

Caught a CustomError!

Minimal reproduction of the problem with instructions

For some reason, I wasn't able to reproduce on Stackblitz, it seems they use some sort of SSR (???), anyway I couldn't see the network request but the component was displayed...

So here a simple repro on Github:
https://github.com/maxime1992/angular-repro-handle-error-custom-loader

  1. take a look to the global error handler

  2. take a look into the custom module loader

The problem is probably located in the custom module loader because if I throw the same error from somewhere else, it's well caught as a CustomError and not a simple Error.

What is the motivation / use case for changing the behavior?

Avoid to search in the error message for a given message and use proper instanceof to know what type is an error.

Environment


Angular CLI: 1.6.7
Node: 9.4.0
OS: linux x64
Angular: 5.2.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.6.7
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.2.0
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.7
typescript: 2.7.1
webpack: 3.10.0
Browser:
- [ x ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimecore: error handlingfreq1: lowtype: bug/fix

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions