Repository navigation
fix: send 500 response code on critical errors #10579
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
Open
shivamdev-lgtm
wants to merge
11
commits into
WordPress:trunk
Choose a base branch
from
shivamdev-lgtm:fix/resp-code-500
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3280d5b
fix: send 500 response code on critical errors
shivamdev-lgtm a246dd0
fix: pr reviews
shivamdev-lgtm de14cfe
fix: remove redundant function_exists check
shivamdev-lgtm d19b7a3
fix: more PR reviews
shivamdev-lgtm c1b1556
Merge branch 'trunk' into fix/resp-code-500
westonruter 4391385
Update src/wp-includes/functions.php
shivamdev-lgtm 756e1d6
chore: remove redundant WP_Error check
shivamdev-lgtm ece64eb
fix: do not send response code if header already sent
shivamdev-lgtm 47cb55a
Merge branch 'trunk' into fix/resp-code-500
audrasjb 64b844a
Merge branch 'trunk' into fix/resp-code-500
shivamdev-lgtm 3bfafa7
test: add tests for HTTP 500 response code in _default_wp_die_handler()
shivamdev-lgtm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for the _default_wp_die_handler() function. | ||
| * | ||
| * These tests run in separate processes so that no output has been sent | ||
| * before `wp_die()` is called, allowing the HTTP response code to be set | ||
| * and inspected. | ||
| * | ||
| * @runTestsInSeparateProcesses | ||
| * @preserveGlobalState disabled | ||
| * | ||
| * @group functions | ||
| * | ||
| * @covers ::_default_wp_die_handler | ||
| */ | ||
| class Tests_Functions_DefaultWpDieHandler extends WP_UnitTestCase { | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
|
|
||
| // Override the test suite's wp_die handler, which throws an exception, with the default handler. | ||
| add_filter( 'wp_die_handler', array( $this, 'get_default_wp_die_handler' ), 20 ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the name of the default wp_die() handler. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_default_wp_die_handler() { | ||
| return '_default_wp_die_handler'; | ||
| } | ||
|
|
||
| /** | ||
| * Simulates the admin header having been output, without running the callbacks hooked to it. | ||
| * | ||
| * In this state, _default_wp_die_handler() does not output its own HTML head or call status_header(). | ||
| */ | ||
| private function mark_admin_head_as_done() { | ||
| global $wp_actions; | ||
|
|
||
| $wp_actions['admin_head'] = 1; | ||
| } | ||
|
|
||
| /** | ||
| * Calls wp_die() without exiting and returns the resulting HTTP response code. | ||
| * | ||
| * @param string|WP_Error $message Error message or WP_Error object. | ||
| * @param array $args Arguments to pass to wp_die(). | ||
| * @return int|false The HTTP response code, or false if none was set. | ||
| */ | ||
| private function get_response_code_from_wp_die( $message, $args = array() ) { | ||
| $args['exit'] = false; | ||
|
|
||
| ob_start(); | ||
| wp_die( $message, '', $args ); | ||
| ob_end_clean(); | ||
|
|
||
| return http_response_code(); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_500_response_code_by_default() { | ||
| $this->assertSame( 500, $this->get_response_code_from_wp_die( 'Error message' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_500_response_code_when_response_is_500() { | ||
| $this->assertSame( 500, $this->get_response_code_from_wp_die( 'Error message', array( 'response' => 500 ) ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_500_response_code_for_internal_server_error_code() { | ||
| $response_code = $this->get_response_code_from_wp_die( | ||
| 'Error message', | ||
| array( | ||
| 'code' => 'internal_server_error', | ||
| 'response' => 403, | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 500, $response_code ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_500_response_code_for_internal_server_error_wp_error() { | ||
| $error = new WP_Error( 'internal_server_error', 'Error message', array( 'status' => 403 ) ); | ||
|
|
||
| $this->assertSame( 500, $this->get_response_code_from_wp_die( $error ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that a critical error sends a 500 response code even after the admin header was output. | ||
| * | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_500_response_code_for_critical_error_after_admin_head() { | ||
| $this->mark_admin_head_as_done(); | ||
|
|
||
| $error = new WP_Error( 'internal_server_error', 'There has been a critical error on this website.' ); | ||
|
|
||
| $this->assertSame( 500, $this->get_response_code_from_wp_die( $error, array( 'response' => 500 ) ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that non-critical errors still do not send a response code after the admin header was output. | ||
| * | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_not_send_response_code_for_non_critical_error_after_admin_head() { | ||
| $this->mark_admin_head_as_done(); | ||
|
|
||
| $response_code = $this->get_response_code_from_wp_die( | ||
| 'Error message', | ||
| array( | ||
| 'code' => 'not_found', | ||
| 'response' => 404, | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertFalse( $response_code ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests the default critical error template of the fatal error handler, after the admin header was output. | ||
| * | ||
| * @ticket 64256 | ||
| * | ||
| * @covers WP_Fatal_Error_Handler::display_default_error_template | ||
| */ | ||
| public function test_fatal_error_handler_default_template_should_send_500_response_code() { | ||
| $this->mark_admin_head_as_done(); | ||
|
|
||
| $error = array( | ||
| 'type' => E_ERROR, | ||
| 'message' => 'Fatal error', | ||
| 'file' => __FILE__, | ||
| 'line' => __LINE__, | ||
| ); | ||
|
|
||
| $method = new ReflectionMethod( 'WP_Fatal_Error_Handler', 'display_default_error_template' ); | ||
| if ( PHP_VERSION_ID < 80100 ) { | ||
| $method->setAccessible( true ); | ||
| } | ||
|
|
||
| ob_start(); | ||
| $method->invoke( new WP_Fatal_Error_Handler(), $error, false ); | ||
| ob_end_clean(); | ||
|
|
||
| $this->assertSame( 500, http_response_code() ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 64256 | ||
| */ | ||
| public function test_should_send_custom_response_code_for_non_critical_errors() { | ||
| $response_code = $this->get_response_code_from_wp_die( | ||
| 'Error message', | ||
| array( | ||
| 'code' => 'not_found', | ||
| 'response' => 404, | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 404, $response_code ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe replace "Ensure" with "Enforce"?