Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ErrorHandler] Add a command to dump static error pages #58769

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

Merged
merged 1 commit into from
Mar 1, 2025

Conversation

pyrech
Copy link
Contributor

@pyrech pyrech commented Nov 5, 2024

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

When a web server cannot handle a request or trigger an error without calling the PHP application, it will return instead its default error pages, ignoring completly the error templates defined by the application (Symfony's default "Oops" error page or overriden one in user's app).

Take for example the case of the simple /% url : it will trigger an error on almost all web servers (nginx, apache, cloudflare, etc):

In all these cases, web servers returned their default error page.

To avoid that, in some of our projects, we created a Symfony command to dump the application error pages in static HTML files that the web server can render when it encounters an internal error. The idea is to dump these pages at deploy time so there is nothing else to do at runtime.

Here is a sample on how we configured our nginx to use our beautiful error pages:

error_page 400 /error_pages/400.html;
error_page 401 /error_pages/401.html;
# ...
error_page 510 /error_pages/510.html;
error_page 511 /error_pages/511.html;

location ^~ /error_pages/ {
  root /path/to/your/symfony/var/cache/error_pages;
  internal; # allows this location block to not be triggered when a user manually call these /error_pages/.* urls
}

(Kudos to @xavierlacot for all the hard work and researches on this topic 💛)

We propose to add this command directly to Symfony so everybody can make use of it.

Usage

bin/console error:dump var/cache/prod/error_page [--force] [400 401 ... 511]

@pyrech pyrech requested a review from yceruto as a code owner November 5, 2024 15:09
@carsonbot carsonbot added this to the 7.2 milestone Nov 5, 2024
@pyrech pyrech force-pushed the dump-error-pages branch 4 times, most recently from 2ce43b0 to 79cfa6e Compare November 5, 2024 15:47
@pyrech pyrech force-pushed the dump-error-pages branch 4 times, most recently from d6d7401 to 0fc7ceb Compare November 12, 2024 20:55
@pyrech
Copy link
Contributor Author

pyrech commented Nov 12, 2024

I have just moved the command in the Twig bundle, applied most of the review suggestions (and mark them as resolved) and answered to the remaining ones.

I don't understand the psalm failure as the arrayNode method of the configuration is used elsewhere without issue. Did I miss something?

@pyrech pyrech force-pushed the dump-error-pages branch 3 times, most recently from 62cd182 to 49aa562 Compare November 12, 2024 21:57
@smnandre
Copy link
Member

I don't understand the psalm failure as the arrayNode method of the configuration is used elsewhere without issue. Did I miss something?

I guess it can be ignored, Psalm does not know the exact type of the "rootNode" here.. and so type it as ParentNode|ArrayDefinition 🤷‍♂️

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@pyrech
Copy link
Contributor Author

pyrech commented Dec 3, 2024

I guess it can be ignored, Psalm does not know the exact type of the "rootNode" here.. and so type it as ParentNode|ArrayDefinition 🤷‍♂️

The Psalm baseline seems to be build automatically so not sure if I have something to do on my side to "ignore" this error?

--

Meanwhile I fixed latest review comments. Is there anything I can do to move forward with PR? 🙂

Thanks

@Jean-Beru
Copy link
Contributor

Meanwhile I fixed latest review comments. Is there anything I can do to move forward with PR? 🙂

If we use semantic configuration to configure this command, we have to add a test in https://github.com/symfony/symfony/blob/7.3/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/ConfigurationTest.php

FYI, we'll also have to update https://github.com/symfony/symfony/blob/7.3/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd.

@carsonbot carsonbot changed the title Add a command to dump static error pages [TwigBundle] Add a command to dump static error pages Jan 6, 2025
@Jean-Beru
Copy link
Contributor

Jean-Beru commented Jan 7, 2025

The Psalm baseline seems to be build automatically so not sure if I have something to do on my side to "ignore" this error?

Is it linked to a missing ->end() after ->prototype('integer')?

The configuration will be:

    private function addErrorDumperSection(ArrayNodeDefinition $rootNode): void
    {
        $rootNode
            ->children()
                ->arrayNode('error_dumper')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->scalarNode('path')
                            ->info('The directory where error pages will be dumped.')
                            ->defaultValue('%kernel.build_dir%/error_pages')
                        ->end()
                        ->arrayNode('status_codes')
                            ->info('The error status codes to dump. By default, all 4xx and 5xx status codes will be dumped.')
                            ->defaultValue([])
                            ->integerPrototype()->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ;
    }

@carsonbot carsonbot changed the title [TwigBundle] Add a command to dump static error pages [ErrorHandler] Add a command to dump static error pages Feb 20, 2025
Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

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

Thanks 👍🏼

@pyrech
Copy link
Contributor Author

pyrech commented Feb 28, 2025

Applied suggestions from @fabpot & @mtarld 👍

However, tests are now failling on Symfony\Bridge\Twig\Tests\NodeVisitor\TranslationDefaultDomainNodeVisitorTest , but I fail to see how this is related to this PR.

@pyrech pyrech force-pushed the dump-error-pages branch 2 times, most recently from 6b9e6fe to 37899b7 Compare February 28, 2025 10:08
@OskarStark
Copy link
Contributor

However, tests are now failling on Symfony\Bridge\Twig\Tests\NodeVisitor\TranslationDefaultDomainNodeVisitorTest , but I fail to see how this is related to this PR.

You can ignore it then.
Checking the 7.3 branch if it is failing there too, is a good double check and if not, a rebase should help 😃

@pyrech pyrech force-pushed the dump-error-pages branch 2 times, most recently from d1a8c60 to b161e14 Compare February 28, 2025 11:17
@fabpot fabpot force-pushed the dump-error-pages branch from b161e14 to 37fe14b Compare March 1, 2025 16:10
@fabpot
Copy link
Member

fabpot commented Mar 1, 2025

Thank you @pyrech.

@fabpot fabpot merged commit 9d84727 into symfony:7.3 Mar 1, 2025
8 of 9 checks passed
@pyrech pyrech deleted the dump-error-pages branch March 1, 2025 16:57
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Mar 10, 2025
This PR was merged into the 7.3 branch.

Discussion
----------

Add documentation for `error:dump` command

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/releases for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `7.x` for features of unreleased versions).

-->
Fix #20704

PR introducing the command: symfony/symfony#58769

Commits
-------

d976d0e Add documentation for error:dump command
@fabpot fabpot mentioned this pull request May 2, 2025
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.