<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[44962] trunk/src: Bootstrap/Load: Introduce fatal error handler.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/44962">44962</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/44962","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-03-21 16:02:05 +0000 (Thu, 21 Mar 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Bootstrap/Load: Introduce fatal error handler.
This changeset introduces a `WP_Fatal_Error_Handler` class that detects fatal errors and displays a more user-friendly message about the site experiencing technical difficulties.
Websites that have custom requirements in that regard can implement their own fatal error handler by adding a `fatal-error-handler.php` drop-in that returns the handler instance to use, which must be based on a class that inherits `WP_Fatal_Error_Handler`. That handler will then be used in place of the default one. Alternatively, the fatal error handler feature can be completely disable through a constant `WP_DISABLE_FATAL_ERROR_HANDLER`.
Websites that would like to modify specifically the error template displayed in the frontend can add a `php-error.php` drop-in that works similarly to the existing `db-error.php` drop-in. For more granular customization, the fatal error handler also includes new filters `wp_should_handle_php_error`, `wp_php_error_message` and `wp_php_error_args`.
Props afragen, bradleyt, flixos90, ocean90, schlessera, SergeyBiryukov, spacedmonkey, timothyblynjacobs.
See <a href="https://core.trac.wordpress.org/ticket/46130">#46130</a>, <a href="https://core.trac.wordpress.org/ticket/44458">#44458</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpfatalerrorhandlerphp">trunk/src/wp-includes/class-wp-fatal-error-handler.php</a></li>
<li><a href="#trunksrcwpincludeserrorprotectionphp">trunk/src/wp-includes/error-protection.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpfatalerrorhandlerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-fatal-error-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-fatal-error-handler.php (rev 0)
+++ trunk/src/wp-includes/class-wp-fatal-error-handler.php 2019-03-21 16:02:05 UTC (rev 44962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,185 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Error Protection API: WP_Fatal_Error_Handler class
+ *
+ * @package WordPress
+ * @since 5.2.0
+ */
+
+/**
+ * Core class used as the default shutdown handler for fatal errors.
+ *
+ * A drop-in 'fatal-error-handler.php' can be used to override the instance of this class and use a custom
+ * implementation for the fatal error handler that WordPress registers. The custom class should extend this class and
+ * can override its methods individually as necessary. The file must return the instance of the class that should be
+ * registered.
+ *
+ * @since 5.2.0
+ */
+class WP_Fatal_Error_Handler {
+
+ /**
+ * Runs the shutdown handler.
+ *
+ * This method is registered via `register_shutdown_function()`.
+ *
+ * @since 5.2.0
+ */
+ public function handle() {
+ // Bail if WordPress executed successfully.
+ if ( defined( 'WP_EXECUTION_SUCCEEDED' ) && WP_EXECUTION_SUCCEEDED ) {
+ return;
+ }
+
+ try {
+ // Bail if no error found.
+ $error = $this->detect_error();
+ if ( ! $error ) {
+ return;
+ }
+
+ // Display the PHP error template.
+ $this->display_error_template();
+ } catch ( Exception $e ) {
+ // Catch exceptions and remain silent.
+ }
+ }
+
+ /**
+ * Detects the error causing the crash if it should be handled.
+ *
+ * @since 5.2.0
+ *
+ * @return array|null Error that was triggered, or null if no error received or if the error should not be handled.
+ */
+ protected function detect_error() {
+ $error = error_get_last();
+
+ // No error, just skip the error handling code.
+ if ( null === $error ) {
+ return null;
+ }
+
+ // Bail if this error should not be handled.
+ if ( ! $this->should_handle_error( $error ) ) {
+ return null;
+ }
+
+ return $error;
+ }
+
+ /**
+ * Determines whether we are dealing with an error that WordPress should handle
+ * in order to protect the admin backend against WSODs.
+ *
+ * @since 5.2.0
+ *
+ * @param array $error Error information retrieved from error_get_last().
+ * @return bool Whether WordPress should handle this error.
+ */
+ protected function should_handle_error( $error ) {
+ $error_types_to_handle = array(
+ E_ERROR,
+ E_PARSE,
+ E_USER_ERROR,
+ E_COMPILE_ERROR,
+ E_RECOVERABLE_ERROR,
+ );
+
+ if ( isset( $error['type'] ) && in_array( $error['type'], $error_types_to_handle, true ) ) {
+ return true;
+ }
+
+ /**
+ * Filters whether a given thrown error should be handled by the fatal error handler.
+ *
+ * This filter is only fired if the error is not already configured to be handled by WordPress core. As such,
+ * it exclusively allows adding further rules for which errors should be handled, but not removing existing
+ * ones.
+ *
+ * @since 5.2.0
+ *
+ * @param bool $should_handle_error Whether the error should be handled by the fatal error handler.
+ * @param array $error Error information retrieved from error_get_last().
+ */
+ return (bool) apply_filters( 'wp_should_handle_php_error', false, $error );
+ }
+
+ /**
+ * Displays the PHP error template and sends the HTTP status code, typically 500.
+ *
+ * A drop-in 'php-error.php' can be used as a custom template. This drop-in should control the HTTP status code and
+ * print the HTML markup indicating that a PHP error occurred. Note that this drop-in may potentially be executed
+ * very early in the WordPress bootstrap process, so any core functions used that are not part of
+ * `wp-includes/load.php` should be checked for before being called.
+ *
+ * If no such drop-in is available, this will call {@see WP_Fatal_Error_Handler::display_default_error_template()}.
+ *
+ * @since 5.2.0
+ */
+ protected function display_error_template() {
+ if ( defined( 'WP_CONTENT_DIR' ) ) {
+ // Load custom PHP error template, if present.
+ $php_error_pluggable = WP_CONTENT_DIR . '/php-error.php';
+ if ( is_readable( $php_error_pluggable ) ) {
+ require_once $php_error_pluggable;
+
+ return;
+ }
+ }
+
+ // Otherwise, display the default error template.
+ $this->display_default_error_template();
+ }
+
+ /**
+ * Displays the default PHP error template.
+ *
+ * This method is called conditionally if no 'php-error.php' drop-in is available.
+ *
+ * It calls {@see wp_die()} with a message indicating that the site is experiencing technical difficulties and a
+ * login link to the admin backend. The {@see 'wp_php_error_message'} and {@see 'wp_php_error_args'} filters can
+ * be used to modify these parameters.
+ *
+ * @since 5.2.0
+ */
+ protected function display_default_error_template() {
+ if ( ! function_exists( '__' ) ) {
+ wp_load_translations_early();
+ }
+
+ if ( ! function_exists( 'wp_die' ) ) {
+ require_once ABSPATH . WPINC . '/functions.php';
+ }
+
+ $message = __( 'The site is experiencing technical difficulties.' );
+
+ $args = array(
+ 'response' => 500,
+ 'exit' => false,
+ );
+
+ /**
+ * Filters the message that the default PHP error template displays.
+ *
+ * @since 5.2.0
+ *
+ * @param string $message HTML error message to display.
+ */
+ $message = apply_filters( 'wp_php_error_message', $message );
+
+ /**
+ * Filters the arguments passed to {@see wp_die()} for the default PHP error template.
+ *
+ * @since 5.2.0
+ *
+ * @param array $args Associative array of arguments passed to `wp_die()`. By default these contain a
+ * 'response' key, and optionally 'link_url' and 'link_text' keys.
+ */
+ $args = apply_filters( 'wp_php_error_args', $args );
+
+ $error = new WP_Error( 'internal_server_error', $message );
+
+ wp_die( $error, '', $args );
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludeserrorprotectionphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/error-protection.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/error-protection.php (rev 0)
+++ trunk/src/wp-includes/error-protection.php 2019-03-21 16:02:05 UTC (rev 44962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,54 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Error Protection API: Functions
+ *
+ * @package WordPress
+ * @since 5.2.0
+ */
+
+/**
+ * Registers the shutdown handler for fatal errors.
+ *
+ * The handler will only be registered if {@see wp_is_fatal_error_handler_enabled()} returns true.
+ *
+ * @since 5.2.0
+ */
+function wp_register_fatal_error_handler() {
+ if ( ! wp_is_fatal_error_handler_enabled() ) {
+ return;
+ }
+
+ $handler = null;
+ if ( defined( 'WP_CONTENT_DIR' ) && is_readable( WP_CONTENT_DIR . '/fatal-error-handler.php' ) ) {
+ $handler = include WP_CONTENT_DIR . '/fatal-error-handler.php';
+ }
+
+ if ( ! is_object( $handler ) || ! is_callable( array( $handler, 'handle' ) ) ) {
+ $handler = new WP_Fatal_Error_Handler();
+ }
+
+ register_shutdown_function( array( $handler, 'handle' ) );
+}
+
+/**
+ * Checks whether the fatal error handler is enabled.
+ *
+ * A constant `WP_DISABLE_FATAL_ERROR_HANDLER` can be set in `wp-config.php` to disable it, or alternatively the
+ * {@see 'wp_fatal_error_handler_enabled'} filter can be used to modify the return value.
+ *
+ * @since 5.2.0
+ *
+ * @return bool True if the fatal error handler is enabled, false otherwise.
+ */
+function wp_is_fatal_error_handler_enabled() {
+ $enabled = ! defined( 'WP_DISABLE_FATAL_ERROR_HANDLER' ) || ! WP_DISABLE_FATAL_ERROR_HANDLER;
+
+ /**
+ * Filters whether the fatal error handler is enabled.
+ *
+ * @since 5.2.0
+ *
+ * @param bool $enabled True if the fatal error handler is enabled, false otherwise.
+ */
+ return apply_filters( 'wp_fatal_error_handler_enabled', $enabled );
+}
</ins></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2019-03-21 15:30:52 UTC (rev 44961)
+++ trunk/src/wp-settings.php 2019-03-21 16:02:05 UTC (rev 44962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,9 +17,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Include files required for initialization.
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/load.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/class-wp-fatal-error-handler.php' );
+require( ABSPATH . WPINC . '/error-protection.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/default-constants.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require_once( ABSPATH . WPINC . '/plugin.php' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Make sure we register the shutdown handler for fatal errors as soon as possible.
+wp_register_fatal_error_handler();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * These can't be directly globalized in version.php. When updating,
</span><span class="cx" style="display: block; padding: 0 10px"> * we're including version.php from another installation and don't want
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -528,3 +533,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'wp_loaded' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/*
+ * Store the fact that we could successfully execute the entire WordPress
+ * lifecycle. This is used to skip the premature shutdown handler, as it cannot
+ * be unregistered.
+ */
+if ( ! defined( 'WP_EXECUTION_SUCCEEDED' ) ) {
+ define( 'WP_EXECUTION_SUCCEEDED', true );
+}
</ins></span></pre>
</div>
</div>
</body>
</html>