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
fix(upgrade): add try/catch when downgrading injectables #38671
Conversation
This commit improves the error thrown by the downgrade module with a more descriptive message on why the downgrade is failing. Closes angular#37579
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); |
There was a problem hiding this comment.
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.
return injector.get(token); | ||
} catch (e) { | ||
throw new Error( | ||
`Trying to get the Angular injector before bootstrapping the corresponding Angular module. Injector name: ${ |
There was a problem hiding this comment.
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}`);
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
const injector: Injector = $injector.get(injectorKey); | ||
return injector.get(token); | ||
} catch (err) { | ||
throw new Error(`Error while ${attemptedAction}: ${err.message || err}`); |
There was a problem hiding this comment.
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).
@sonukapoor, as discussed "offline", I added a fixup commit with my suggestions. LMK what you think. |
Thanks for making the changes @gkalpak. This LGTM. |
@AndrewKushnir Can you please run a presubmit? |
FYI, presubmit went well. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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?
What is the current behavior?
Issue Number: #37579
Does this PR introduce a breaking change?