-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Add logging to contact us form #9343
Add logging to contact us form #9343
Conversation
*/ | ||
public function __construct( | ||
Context $context, | ||
ConfigInterface $contactsConfig, | ||
MailInterface $mail, | ||
DataPersistorInterface $dataPersistor | ||
DataPersistorInterface $dataPersistor, | ||
LoggerInterface $logger |
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.
Due to backwards compatibility policy we can not add a new required argument to the constructor.
You may use the injection with the optional parameter, like this:
public function __construct(\NewDependencyInterface $newDependency = null) {
$this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\NewDependencyInterface::class);
}
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.
Instantiate the object directly with object manager? My understanding is that is a big no no
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.
Regardless, updated. Would like clarification on when it is ok to use object manager. Maybe only in core code?
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.
You are right, using ObjectManager directly is indeed a "big no no". However, some existing extension or customization may possibly extend this class, thus, by changing the constructor signature you would break it.
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.
Such direct usages of the ObjectManager should be refactored eventually, but that may only happen we module version is bumped.
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.
Makes sense, thanks for the clarification!
*/ | ||
public function __construct( | ||
Context $context, | ||
ConfigInterface $contactsConfig, | ||
MailInterface $mail, | ||
DataPersistorInterface $dataPersistor | ||
DataPersistorInterface $dataPersistor, |
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.
current parameter should be optional as well.
$dataPersistor = null
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.
What is the reasoning behind this?
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.
Addressed. I'm concerned about the future look of M2 if these kind of changes continue though. Is the plan just to have optionally parameterized constructors on every class with direct calls to object manager?
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.
we have next requirements dictated by Backward Compatibility policy:
Add the new optional parameter to the constructor at the end of arguments list.
In the constructor body, if new dependency is not provided (value of introduced argument is null) fetch the dependency using Magento\Framework\App\ObjectionManager::getInstance().
class ExistingClass
{
/** @var \New\Dependency\Interface */
private $newDependency;
public function __construct(
\Old\Dependency\Intreface $oldDependency,
$oldRequiredConstructorParameter,
$oldOptinalConstructorParameter = null,
\New\Dependency\Interface $newDependency = null
) {
...
$this->newDependency = $newDependency ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\New\Dependency\Interface::class);
}
public function existingFunction()
{
// Existing functionality
...
...
// Use $this->newDependency wherever the new dependency is needed
...
...
}
}
// Sample unit test code snippet follows
class ExistingClassTest extends \PHPUnit_Framework_TestCase
{
private $existingClassObject;
protected function setUp()
{
...
// Create dependency mcoks with $this->getMock() or $this->getMockBuilder()
$newDependencyMock = $this->getMock(\New\Dependency\Interface::class, [], [], '', false);
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->existingClassObject = $objectManager->getObject(
ExistingClass::class,
[
'oldDependency' => $oldDependencyMock,
'oldRequiredConstructorParameter' => 'foo',
'oldOptinalConstructorParameter' => 'bar',
'newDependency' => $newDependencyMock,
]
);
}
public function testExistingFunction()
{
...
...
}
}
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.
@JamesonNetworks oh sorry, my fault
I didn't notice that this parameter has already been there before.
DataPersistorInterface $dataPersistor
Sorry, for misleading you.
I was wrong. No need to make it optional.
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.
rgr, no worries, corrected and pushed
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'm not confused about what you are doing, I'm confused as to why. It seems to me you would just mark the PRs that change the constructors and tag them to be released in releases that have the potential to break compatibility. Instead, we're building in tech debt here and expecting a maintainer to come in behind and remove these problems at the time of releases
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.
adding an optional parameter to the constructor doesn't bring Backward Incompatible changes. Because code will work according to its contract nevertheless dependency was passed or not (in this case we will instantiate one with ObjectManager::getInstance static call)
@@ -70,8 +79,10 @@ public function execute() | |||
$this->messageManager->addErrorMessage($e->getMessage()); | |||
$this->getDataPersistor()->set('contact_us', $this->getRequest()->getParams()); | |||
} catch (\Exception $e) { | |||
$this->logger->addError('An error occurred in Controller\\Index\\Post: ' . $e->getMessage()); |
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.
a) why do we need this one? wouldn't the stack trace by enough?
b) if you're going to do it, why aren't you using __CLASS__
?
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.
a) I'm not sure what M2 logging best practices are so I was throwing it in the two places I would expect to see it.
b) Should have used __CLASS__
here.
I removed this line since the critical log could be enough? If you have any pointers to docs on best practices for core logging, I would appreciate it.
Thanks!
@adragus-inviqa @ishakhsuvarov @maghamed Thanks everyone for the review and input! I appreciate the feedback |
@JamesonNetworks thank you for your contribution. Your Pull Request has been successfully merged |
Description
If contact us fails to process, the exception is swallowed
Fixed Issues (if relevant)
I did not open an issue for this problem
Manual testing scenarios
Contribution checklist