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

magento/magento2#20773: Do not throw exception during autoload #20950

Merged

Conversation

Vinai
Copy link
Contributor

@Vinai Vinai commented Feb 3, 2019

Description (*)

Avoid uncaught exceptions during autoloading. PSR-4 dictates that must not happen. Return false instead.
Instead, try to log the exception message.

Fixed Issues (if relevant)

#20773

Manual testing scenarios (*)

Run class_exists(FooFactory::class);.
More information to reproduce the problem is in the issue thread.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @Vinai. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@davidverholen
Copy link
Member

Hey @Vinai , since autoloader registration happens before the objectmanager is fully initialized in \Magento\Framework\App\ObjectManagerFactory::create, it now (ironically) throws an exception during the construction of the autoloader.

Instead you would have to manually create the logger instance and inject it here \Magento\Framework\ObjectManager\DefinitionFactory::createClassDefinition

Not sure how complicated it is to initialize the logger without the OM. Unfortunately I did not find it in any of the calling classes to maybe pass it through.

Just another thing: Wouldn't it be more helpful (even if it's nor psr4 compliant) to keep the exceptions at least in developer mode so they are just displayed directly instead of "hidden" in a log file?

@Vinai
Copy link
Contributor Author

Vinai commented Feb 3, 2019

He, stupid of me, sorry. The contribution day ended so I ran out of time to test it.
I’m going to fix it tomorrow.
Thanks!

@Vinai Vinai force-pushed the 20773-autoloader-exception branch from 3e78c3a to ce91cc1 Compare February 4, 2019 05:37
@Vinai
Copy link
Contributor Author

Vinai commented Feb 4, 2019

This was a typical case of Unit Tests green, missing integration tests.
Fixed now :)

@Vinai Vinai force-pushed the 20773-autoloader-exception branch 2 times, most recently from 721c8ac to 7ab3ee4 Compare February 4, 2019 07:57
@Vinai
Copy link
Contributor Author

Vinai commented Feb 4, 2019

@davidverholen I decided against letting the exception bubble up in developer mode, because that still would prohibit using another autoloader later in the autoloader chain (which is the original reason for the PR). Instead, the code now just tries to log the message if the ObjectManager has been initialized, otherwise there is nothing that can be done and the exception message is lost.
In almost all cases the logger should be available during a generation failure.

@magento-engcom-team
Copy link
Contributor

Hi @davidverholen, thank you for the review.
ENGCOM-4104 has been created to process this Pull Request

@Vinai
Copy link
Contributor Author

Vinai commented Feb 4, 2019

Thanks for reviewing, @davidverholen! I just noticed some typos and couldn't resist fixing them and updating the PR. Sorry if that means you have to repeat the review - at least it's a small patch.

*/
private function getTestFrameworkObjectManager()
{
return ObjectManager::getInstance();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please inline this method? I don't see how it improves readability and also ObjectManager::getInstance() is used as is at least once.

Copy link
Contributor Author

@Vinai Vinai Feb 4, 2019

Choose a reason for hiding this comment

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

The return type hint fixes the type inference in PHPStorm (see screenshot).
typeinference

Copy link
Contributor Author

@Vinai Vinai Feb 4, 2019

Choose a reason for hiding this comment

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

Apropos: I should make a PR to fix the return annotation on getInstance on the test framework ObjectManager - this has been annoying me for 2.5 years now.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Vinai yep, there is no need to bloat each single test.

/**
* @after
*/
public function removeLoggerTestDouble(): void
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just call them setUp/tearDown? Why public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer not to override parent class methods. The @before and @after annotations on public methods with descriptive names are preferable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Such name does not add any value compared to just setUp. I have nothing against such annotations in general but then we should make it an official recommendation, otherwise the code become inconsistent.

@orlangur orlangur self-assigned this Feb 4, 2019
@Vinai Vinai force-pushed the 20773-autoloader-exception branch 4 times, most recently from 010a9ac to d45eded Compare February 5, 2019 16:01
@Vinai Vinai force-pushed the 20773-autoloader-exception branch from d45eded to cab38a2 Compare February 11, 2019 09:56
@Vinai
Copy link
Contributor Author

Vinai commented Feb 11, 2019

The warnings that are causing Codacy to fail are unrelated to the PR, but because of outdated rulesets.
Screenshot of the warnings regarding outdated rulesets

@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, thank you for the review.
ENGCOM-4104 has been created to process this Pull Request

@Vinai
Copy link
Contributor Author

Vinai commented Feb 17, 2019

I think the failing test is unrelated to this PR.
Is there anything I can do to move this PR forward?

@ghost
Copy link

ghost commented Feb 25, 2019

Hi @Vinai, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@Vinai
Copy link
Contributor Author

Vinai commented Feb 25, 2019

Thanks for merging 👍 👍

I'd like to take the survey, but the app wants full write access to all my orgs:

bildschirmfoto 2019-02-25 um 06 17 34

This seems like a bad idea, so I'll give it a pass.

@orlangur
Copy link
Contributor

@Vinai LOOOL :) Thanks for pointing out, I'll pass it to EngCom guys.

@Vinai
Copy link
Contributor Author

Vinai commented Feb 25, 2019

The same survey app is used for devdoc PRs, too, by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants