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

Conversation

sonukapoor
Copy link
Contributor

This commit improves the error thrown by the downgrade module with a more
descriptive message on why the downgrade is failing.

Closes #37579

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

Issue Number: #37579

Does this PR introduce a breaking change?

  • Yes
  • No

This commit improves the error thrown by the downgrade module with a more
descriptive message on why the downgrade is failing.

Closes angular#37579
@sonukapoor sonukapoor added the area: upgrade Issues related to AngularJS → Angular upgrade APIs label Sep 2, 2020
@ngbot ngbot bot added this to the needsTriage milestone Sep 2, 2020
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.

gkalpak
gkalpak previously requested changes Sep 3, 2020
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}`);

@googlebot

This comment has been minimized.

@googlebot googlebot added cla: no and removed cla: yes labels Sep 6, 2020
@gkalpak

This comment has been minimized.

@googlebot

This comment has been minimized.

@googlebot googlebot added cla: yes and removed cla: no labels Sep 6, 2020
@gkalpak gkalpak dismissed their stale review September 6, 2020 17:33

Comments addressed.

const injector: Injector = $injector.get(injectorKey);
return injector.get(token);
} catch (err) {
throw new Error(`Error while ${attemptedAction}: ${err.message || err}`);
Copy link
Member

Choose a reason for hiding this comment

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

I realized that some browsers (for example, Firefox) do not include the message in err.stack. So, I switched to err.message || err. There shouldn't be anything too useful in the stacktrace for app developers anyway (it's mostly framework DI internals).

@gkalpak
Copy link
Member

gkalpak commented Sep 6, 2020

@sonukapoor, as discussed "offline", I added a fixup commit with my suggestions. LMK what you think.

@sonukapoor
Copy link
Contributor Author

@sonukapoor, as discussed "offline", I added a fixup commit with my suggestions. LMK what you think.

Thanks for making the changes @gkalpak. This LGTM.
@petebacondarwin Can you review again?

@petebacondarwin petebacondarwin added action: presubmit The PR is in need of a google3 presubmit target: patch This PR is targeted for the next patch release labels Sep 7, 2020
@sonukapoor
Copy link
Contributor Author

@AndrewKushnir Can you please run a presubmit?

@AndrewKushnir
Copy link
Contributor

Presubmit.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Sep 9, 2020
@AndrewKushnir
Copy link
Contributor

FYI, presubmit went well.

@sonukapoor sonukapoor added the action: merge The PR is ready for merge by the caretaker label Sep 10, 2020
AndrewKushnir pushed a commit that referenced this pull request Sep 10, 2020
This commit improves the error thrown by the downgrade module with a more
descriptive message on why the downgrade is failing.

Closes #37579

PR Close #38671
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: upgrade Issues related to AngularJS → Angular upgrade APIs cla: yes target: patch This PR is targeted for the next patch release
Projects
None yet
5 participants