Set up URLs to test a variety of error pages, and how your WordPress site will handle warnings and notices as well. This doesn’t do an awful lot for a simple WordPress site, but if you are running through a CDN, updating your logging, or sending data to analytics it can give you a tool to intentionally send error states for testing those tools.
<?php
/*
Plugin Name: Throw Stuff
Description: Generate errors through wordpress to verify debugging
Version: 0.1
Author: Katherine Semel
*/
class ThrowStuff {
function __construct() {
add_action( 'template_redirect', array( $this, 'set_endpoints' ) );
}
function set_endpoints() {
// Give this a status code and it will give it back.
// Make sure it's a number!
if ( strpos( $_SERVER['REQUEST_URI'], 'test/httpcode' ) == 1 ) {
$path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$pathFragments = explode( '/', $path );
$request_code = end( $pathFragments );
if ( is_int( intval( $request_code ) ) ) {
status_header( intval( $request_code ) );
} else {
status_header( 200 );
}
echo $this->error_body( intval( $request_code ) );
die();
}
if ( strpos( $_SERVER['REQUEST_URI'], 'test/notice' ) == 1 ) {
// E_USER_NOTICE - this is the equivalent of E_NOTICE and
// issues a notice message.
// Execution of the script is not halted.
trigger_error( 'Test Generic E_USER_NOTICE', E_USER_NOTICE );
status_header( 200 );
echo $this->error_body( 'Test Error: Notice' );
die();
}
if ( strpos( $_SERVER['REQUEST_URI'], 'test/warning' ) == 1 ) {
// E_USER_WARNING - this is the equivalent of E_WARNING and
// issues a warning message.
// Execution of the script is not halted.
trigger_error( 'Test Generic E_USER_WARNING', E_USER_WARNING );
status_header( 200 );
echo $this->error_body( 'Test Error: Warning' );
die();
}
if ( strpos( $_SERVER['REQUEST_URI'], 'test/error' ) == 1 ) {
// E_USER_ERROR - this is the equivalent of E_ERROR and
// indicates an error that can not be recovered from.
// Execution of the script is halted at this point.
trigger_error( 'Test Generic E_USER_ERROR', E_USER_ERROR );
status_header( 500 );
echo $this->error_body( 'Test Error: Error/Fatal' );
die();
}
}
function error_body( $error_code_or_message ) {
ob_start();
?>
<html>
<head>
<title>Response Code <?php echo strval( $error_code_or_message ); ?></title>
</head>
<body>
<?php
if ( is_int( $error_code_or_message ) ) {
echo "I am an error testing page for code $error_code_or_message";
} else {
echo strval( $error_code_or_message );
}
?>
</body>
</html>
<?php
$output = ob_get_clean();
return $output;
}
}
// Fuck it, let's test in production
// Make sure you understand what you are doing!
$ThrowStuff = new ThrowStuff();