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

Skip to content

fix: send 500 response code on critical errors#10579

Open
shivamdev-lgtm wants to merge 9 commits into
WordPress:trunkfrom
shivamdev-lgtm:fix/resp-code-500
Open

fix: send 500 response code on critical errors#10579
shivamdev-lgtm wants to merge 9 commits into
WordPress:trunkfrom
shivamdev-lgtm:fix/resp-code-500

Conversation

@shivamdev-lgtm

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Dec 2, 2025

Copy link
Copy Markdown

Hi @shivamdev-lgtm! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

github-actions Bot commented Dec 2, 2025

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props whiteshadow01, westonruter, audrasjb, mindctrl.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Dec 2, 2025

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.


$xml = <<<EOD
<error>
<code>{$parsed_args['code']}</code>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like this change needs to be reverted?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

reverted

Comment thread src/wp-includes/functions.php Outdated
// Also ensure HTTP 500 if response is already set to 500 (from fatal error handler).
if ( $is_critical_error || 500 === $parsed_args['response'] ) {
$parsed_args['response'] = 500;
if ( function_exists( 'http_response_code' ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When would this function ever not exist?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

removed the check

Comment thread src/wp-includes/functions.php Outdated
// Also ensure HTTP 500 if response is already set to 500 (from fatal error handler).
if ( $is_critical_error || 500 === $parsed_args['response'] ) {
$parsed_args['response'] = 500;
if ( function_exists( 'http_response_code' ) ) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems weird to set $parsed_args['response'] = 500; if it's already set that way, as per the check on the line above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

simplified

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See comments.

Comment thread src/wp-includes/functions.php Outdated

// Ensure HTTP 500 status code for critical errors, even if headers were already sent.
$is_critical_error = false;
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this function_exists check necessary? It's defined in load.php, which is loaded before this file.

References:

require ABSPATH . WPINC . '/load.php';

require ABSPATH . WPINC . '/functions.php';

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

removed the check

Comment thread src/wp-includes/functions.php Outdated
Comment on lines +3892 to +3893
$error_code = $message->get_error_code();
$is_critical_error = ( 'internal_server_error' === $error_code );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No need to save the error code as a variable since it's only used once.

Suggested change
$error_code = $message->get_error_code();
$is_critical_error = ( 'internal_server_error' === $error_code );
$is_critical_error = ( 'internal_server_error' === $message->get_error_code() );

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

Comment thread src/wp-includes/functions.php Outdated

// Ensure HTTP 500 status code for critical errors, even if headers were already sent.
$is_critical_error = false;
if ( is_wp_error( $message ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All of the function_exists() checks above for __() and esc_url() make me think that actually we should maybe not assume that is_wp_error() exists. In any case, it isn't necessary:

Suggested change
if ( is_wp_error( $message ) ) {
if ( $message instanceof WP_Error ) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Updates the default wp_die() HTML handler to force an HTTP 500 status code when handling “critical”/internal server error scenarios.

Changes:

  • Adds “critical error” detection in _default_wp_die_handler() based on code/response (and attempted WP_Error) inputs.
  • Forces the computed wp_die() response to 500 and calls http_response_code(500) for critical errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wp-includes/functions.php Outdated
Comment thread src/wp-includes/functions.php Outdated
Comment on lines +3891 to +3895
if ( $message instanceof WP_Error ) {
$is_critical_error = $message->get_error_code();
} elseif ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) {
$is_critical_error = true;
}

Copilot AI Jan 29, 2026

Copy link

Choose a reason for hiding this comment

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

$message instanceof WP_Error can fatally error if WP_Error is not loaded yet (wp-settings.php loads wp-includes/functions.php before wp-includes/class-wp-error.php). Also, _wp_die_process_input() typically converts a WP_Error message into a string before this handler runs, so this branch is usually unreachable. Consider removing the instanceof check and relying on $parsed_args['code']/$parsed_args['response'], or guard it with class_exists( 'WP_Error' ) before using instanceof.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not entirely correct. If WP_Error does not exist, no fatal error occurs. See https://3v4l.org/uCFvc#veol

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

https://github.com/shivamdev-lgtm/wordpress-develop/blob/756e1d67fd188e35f6b6b04c8df61a0a25d420e5/src/wp-includes/functions.php#L4317-L4318 Since WP_Error is getting converted here, removing the redundant check and simplifying the logic a bit

Comment thread src/wp-includes/functions.php Outdated

if ( $is_critical_error ) {
$parsed_args['response'] = 500;
http_response_code( 500 );

Copilot AI Jan 29, 2026

Copy link

Choose a reason for hiding this comment

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

http_response_code( 500 ) is executed unconditionally when $is_critical_error is true. If headers have already been sent, this can emit a PHP warning (and still cannot change the response code). To avoid noisy warnings/regressions, only call this when ! headers_sent() (or drop it and rely on the existing status_header() call inside the ! headers_sent() block).

Suggested change
http_response_code( 500 );
if ( ! headers_sent() ) {
http_response_code( 500 );
}

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

@mindctrl

mindctrl commented Feb 4, 2026

Copy link
Copy Markdown

@shivamdev-lgtm are you able to address the remaining review comments?

@shivamdev-lgtm

Copy link
Copy Markdown
Author

Yes I'll take it up tomorrow @mindctrl

@shivamdev-lgtm

Copy link
Copy Markdown
Author

@mindctrl I have fixed the rest of the comments. Please take a look

$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
}

// Ensure HTTP 500 status code for critical errors.

Copy link
Copy Markdown
Contributor

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"?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants