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 exception handling inconsistency between developer and production modes #37712

Conversation

iGerchak
Copy link
Contributor

@iGerchak iGerchak commented Jul 4, 2023

Description (*)

During developing our custom module was found an issue in the developer mode, for example, after implementing \Magento\Customer\Controller\AccountInterface class into the custom controller we got redirected to the login page (as was expected), but on the production on the same flow we got an internal server error 500.
It happened because the redirect URL was set to the response before the trigger exception and as a result Location header was sent to the browser and the browser redirect to the login page instead of the page with an exception message.
It's not expected flow between developer and production modes and can cause some issues for production.

Preconditions and environment

  • Magento 2.4.5
  • Controller, which sets a redirect and throws an exception

Steps to reproduce

  1. Create a module:

app/code/Vendor/Module/registration.php:

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);

app/code/Vendor/Module/etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module"/>
</config>

app/code/Vendor/Module/etc/frontend/routes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="vendor" frontName="vendor">
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

magento2/app/code/Vendor/Module/Controller/Example/Index.php:

<?php

namespace Vendor\Module\Controller\Example;

use LogicException;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\SessionException;

class Index implements HttpGetActionInterface
{
    public function __construct(private readonly Session $customerSession,)
    {
    }

    /**
     * Execute action based on request and return result
     *
     * @return ResultInterface|ResponseInterface
     * @throws LogicException
     * @throws SessionException
     */
    public function execute()
    {
        // Here, you will be redirected to the login page when not logged in.
        $this->customerSession->authenticate();

        throw new LogicException(__('Some exception text'));
    }
}
  1. Go to www.domain.com/vendor/example

Expected result

  • Page with exception text like this one:

Capture 2023-07-04 at 14 15 49

Actual result

  • Redirect to the login page

Unfortunately, I don't have enough experience with test coverage, will be great if someone covers this case by test.

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)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Fix exception handling inconsistency between developer and production modes #38639: Fix exception handling inconsistency between developer and production modes

@m2-assistant
Copy link

m2-assistant bot commented Jul 4, 2023

Hi @iGerchak. Thank you for your contribution!
Here are some useful tips on 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 give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@ihor-sviziev ihor-sviziev added Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it Priority: P2 A defect with this priority could have functionality issues which are not to expectations. labels Jul 4, 2023
@m2-community-project m2-community-project bot added this to Pending Review in Pull Requests Dashboard Jul 4, 2023
@ihor-sviziev ihor-sviziev changed the title Fix issue with handle exception in the developer mode Fix issue with exception handling in the developer mode Jul 4, 2023
@ihor-sviziev ihor-sviziev self-assigned this Jul 4, 2023
@m2-community-project m2-community-project bot moved this from Pending Review to Review in Progress in Pull Requests Dashboard Jul 4, 2023
@ihor-sviziev
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@ihor-sviziev ihor-sviziev changed the title Fix issue with exception handling in the developer mode Fix issue with exception handling in developer mode Jul 4, 2023
@ihor-sviziev ihor-sviziev changed the title Fix issue with exception handling in developer mode Fix exception handling inconsistency between developer and production modes Jul 4, 2023
@m2-community-project m2-community-project bot moved this from Review in Progress to Ready for Testing in Pull Requests Dashboard Jul 4, 2023
@ihor-sviziev
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests CE, Functional Tests EE, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@ihor-sviziev
Copy link
Contributor

Test failures look not related

@ihor-sviziev ihor-sviziev added Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests Award: bug fix labels Jul 7, 2023
@ihor-sviziev
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests CE, Functional Tests EE, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@ihor-sviziev
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests EE, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@ihor-sviziev ihor-sviziev force-pushed the fix-issue-with-handle-exception branch from 41d1ff1 to ed2c0ec Compare July 17, 2023 12:24
@ihor-sviziev
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Charlie
Copy link
Contributor

Hi @iGerchak,

Thank you for your contribution!

In order to proceed further on this PR, I have followed the mentioned steps on 2.4-develop on developer mode
1 Created a custom module "testmodule"
2 Added routes.xml, modules.xml and new controller app/code/Magento/TestModule/Controller/Example/Index.php
3 Browsed http://mag.local/testmodule/example/


✔️ QA Passed

Before: ✖️

  • Redirecting to the login page
image

After: ✔️

  • Getting exception on the page
image

Since the build is failing, currently moving it further in Extended Testing.

Thank you!

@engcom-Charlie engcom-Charlie moved this from Testing in Progress to Extended testing (optional) in Community Dashboard Apr 16, 2024
@engcom-Charlie
Copy link
Contributor

@magento run all tests

@engcom-Charlie
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests EE, WebAPI Tests

@engcom-Charlie
Copy link
Contributor

@magento run WebAPI Tests

@engcom-Charlie
Copy link
Contributor

The WebAPI test failures in recent 2 runs are different. Also the test failure in recent run is a known failure. It is not failing because of this PR changes hence moving it to Merge in Progress.

Run 1:
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/37712/7d714a34f5249ec2ef21cbdf52987c1f/WebApi/console-error-logs.html
image

image

Run 2:
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/37712/6cc33d3a0fd45b539562081ff8412aa5/WebApi/console-error-logs.html
image

AC-11750 - testGetCustomerDescendingSortedOrders

@engcom-Charlie engcom-Charlie moved this from Extended testing (optional) to Merge in Progress in Community Dashboard Apr 17, 2024
@engcom-Charlie
Copy link
Contributor

@magento create issue

@m2-community-project m2-community-project bot moved this from Merge in Progress to Ready for Testing in Community Dashboard Apr 19, 2024
@engcom-Charlie engcom-Charlie moved this from Ready for Testing to Merge in Progress in Community Dashboard Apr 19, 2024
@magento-devops-reposync-svc magento-devops-reposync-svc merged commit 1d9a291 into magento:2.4-develop May 28, 2024
9 of 12 checks passed
@engcom-Echo engcom-Echo moved this from Merge in Progress to Recently Merged in Community Dashboard May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests Award: bug fix Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: accept Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Community Dashboard
Recently Merged
Development

Successfully merging this pull request may close these issues.

[Issue] Fix exception handling inconsistency between developer and production modes
5 participants