diff --git a/packages/upgrade/src/common/src/downgrade_injectable.ts b/packages/upgrade/src/common/src/downgrade_injectable.ts index 35a6e11353..af2c7a20bd 100644 --- a/packages/upgrade/src/common/src/downgrade_injectable.ts +++ b/packages/upgrade/src/common/src/downgrade_injectable.ts @@ -79,8 +79,12 @@ 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 (err) { + throw new Error(`Error while ${attemptedAction}: ${err.message || err}`); + } }; (factory as any)['$inject'] = [$INJECTOR]; diff --git a/packages/upgrade/src/common/test/downgrade_injectable_spec.ts b/packages/upgrade/src/common/test/downgrade_injectable_spec.ts index 45f6ddb10e..b2ea6a602e 100644 --- a/packages/upgrade/src/common/test/downgrade_injectable_spec.ts +++ b/packages/upgrade/src/common/test/downgrade_injectable_spec.ts @@ -48,4 +48,16 @@ describe('downgradeInjectable', () => { expect(factory(mockNg1Injector)).toEqual('service value'); expect(mockNg2Injector.get).toHaveBeenCalledWith('someToken'); }); + + it('should mention the injectable\'s name in the error thrown when failing to retrieve injectable', + () => { + const factory = downgradeInjectable('someToken'); + expect(factory).toEqual(jasmine.any(Function)); + expect((factory as any).$inject).toEqual([$INJECTOR]); + + const {mockNg1Injector, mockNg2Injector} = setupMockInjectors(); + mockNg2Injector.get.and.throwError('Mock failure'); + expect(() => factory(mockNg1Injector)) + .toThrowError(/^Error while instantiating injectable 'someToken': Mock failure/); + }); });