Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created September 21, 2012 09:21
Show Gist options
  • Save k-holy/3760553 to your computer and use it in GitHub Desktop.
Save k-holy/3760553 to your computer and use it in GitHub Desktop.
auto_prepend_fileでエラー制御
php_value auto_prepend_file __prepend.php
<?php
namespace Acme;
class HttpException extends \Exception {};
$logfile = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'php_error.log';
set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) use ($logfile) {
$labels = array(
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_STRICT => 'Strict standards',
E_RECOVERABLE_ERROR => 'Catchable fatal error',
E_DEPRECATED => 'Depricated',
E_USER_ERROR => 'User Fatal error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_USER_DEPRECATED => 'User Depricated',
);
$message = sprintf('%s[%d]: %s', isset($labels[$errno]) ? $labels[$errno] : '', $errno, $errstr);
if (!(error_reporting() & $errno)) {
error_log(sprintf("[%s] %s\n", date('Y-m-d H:i:s'), $message), 3, $logfile);
return false;
}
throw new \ErrorException($message, 0, $errno, $errfile, $errline);
});
set_exception_handler(function(\Exception $e) use ($logfile) {
$status = 500;
$title = 'Internal Server Error';
if ($e instanceof HttpException) {
$status = $e->getCode();
switch ($status) {
case 400:
$title = 'Bad Request';
break;
case 403:
$title = 'Forbidden';
break;
case 404:
$title = 'Not Found';
break;
default:
break;
}
}
$message = sprintf("Uncaught Exception %s[%d]: '%s' in %s on line %d\n%s",
get_class($e),
$e->getCode(),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
);
error_log(sprintf("[%s] %s\n", date('Y-m-d H:i:s'), $message), 3, $logfile);
$message = nl2br(htmlspecialchars($message, ENT_QUOTES, 'UTF-8'));
$content = <<< CONTENT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,nofollow" />
<title>{$title}</title>
</head>
<body>
<h1>{$title}</h1>
<p>{$message}</p>
</body>
</html>
CONTENT;
header(sprintf('HTTP/1.1 %d %s', $status, $title));
echo $content;
exit;
});
<?php
error_reporting(E_ALL &~E_USER_NOTICE);
if (isset($_POST['exception']) && isset($_POST['status']) && ctype_digit($_POST['status'])) {
throw new \Acme\HttpException('Test', intval($_POST['status']));
} elseif (isset($_POST['error'])) {
trigger_error('Test', E_USER_ERROR);
} elseif (isset($_POST['warning'])) {
trigger_error('Test', E_USER_WARNING);
} elseif (isset($_POST['notice'])) {
trigger_error('Test', E_USER_NOTICE);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title>auto_prepend_fileでエラー制御</title>
</head>
<body>
<form action="<?=htmlspecialchars($_SERVER['SCRIPT_NAME'], ENT_QUOTES, 'UTF-8')?>" method="post">
<label><input type="radio" name="status" value="400" />400</label>
<label><input type="radio" name="status" value="403" />403</label>
<label><input type="radio" name="status" value="404" />404</label>
<label><input type="radio" name="status" value="500" />500</label>
<input type="submit" name="exception" value="Exception" />
<input type="submit" name="error" value="User Error" />
<input type="submit" name="warning" value="User Warning" />
<input type="submit" name="notice" value="User Notice" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment