Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upgrade): add try/catch when downgrading injectables #38671

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/upgrade/src/common/src/downgrade_injectable.ts
Expand Up @@ -79,8 +79,14 @@ export function downgradeInjectable(token: any, downgradedModule: string = ''):

validateInjectionKey($injector, downgradedModule, injectorKey, attemptedAction);

const injector: Injector = $injector.get(injectorKey);
return injector.get(token);
try {
const injector: Injector = $injector.get(injectorKey);
return injector.get(token);
} catch (e) {
throw new Error(
`Trying to get the Angular injector before bootstrapping the corresponding Angular module. Injector name: ${
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of issues with this error:

  • We don't know (and should not assume) why the operation failed. We should state what we know (that we failed to retrieve token) and keep the original error's message (which has more info on the actual error).
  • token is not the name of the injector. It is the identifier of the injectable.

I would suggest something like:

throw new Error(`Failed to retrieve injectable '${token}' due to: ${e.message || e}`);

Or I would even go for:

throw new Error(`Failed to retrieve injectable '${token}' due to: ${e.stack || e.message || e}`);

token}`);
}
};
(factory as any)['$inject'] = [$INJECTOR];

Expand Down
12 changes: 12 additions & 0 deletions packages/upgrade/src/common/test/downgrade_injectable_spec.ts
Expand Up @@ -48,4 +48,16 @@ describe('downgradeInjectable', () => {
expect(factory(mockNg1Injector)).toEqual('service value');
expect(mockNg2Injector.get).toHaveBeenCalledWith('someToken');
});

it('should throw an error if injector is undefined', () => {
const factory = downgradeInjectable('someToken', 'someModule');
expect(factory).toEqual(jasmine.any(Function));
expect((factory as any).$inject).toEqual([$INJECTOR]);

const {mockNg1Injector} = setupMockInjectors('someModule');
const error =
'Trying to get the Angular injector before bootstrapping the corresponding Angular module. Injector name: someToken';
mockNg1Injector.get.and.throwError(error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test would pass without the fix, since you are specifying the new error message in the mock error that is thrown, yes?

Perhaps make the mock error that is thrown match the old error message - i.e. Trying to get the Angular injector before bootstrapping the corresponding Angular module.. Then test that the actual error that is thrown is the new message.

expect(() => factory(mockNg1Injector)).toThrowError(error);
});
});