change error handler

This commit is contained in:
joyqi 2021-08-20 16:30:16 +08:00
parent 30baad977c
commit 687ab6260a
4 changed files with 72 additions and 118 deletions

View File

@ -287,64 +287,39 @@ class Typecho_Common
spl_autoload_register(['Typecho_Common', '__autoLoad']);
/** 设置异常截获函数 */
set_exception_handler(['Typecho_Common', 'exceptionHandle']);
}
/**
* 异常截获函数
*
* @access public
*
* @param $exception 截获的异常
*
* @return void
*/
public static function exceptionHandle($exception)
{
if (defined('__TYPECHO_DEBUG__') && __TYPECHO_DEBUG__) {
echo '<pre><code>';
echo '<h1>' . htmlspecialchars($exception->getMessage()) . '</h1>';
echo htmlspecialchars($exception->__toString());
echo '</code></pre>';
} else {
@ob_end_clean();
if (404 == $exception->getCode() && !empty(self::$exceptionHandle)) {
$handleClass = self::$exceptionHandle;
new $handleClass($exception);
set_exception_handler(function (Throwable $exception) {
if (defined('__TYPECHO_DEBUG__') && __TYPECHO_DEBUG__) {
echo '<pre><code>';
echo '<h1>' . htmlspecialchars($exception->getMessage()) . '</h1>';
echo htmlspecialchars($exception->__toString());
echo '</code></pre>';
} else {
self::error($exception);
@ob_end_clean();
if (404 == $exception->getCode() && !empty(self::$exceptionHandle)) {
$handleClass = self::$exceptionHandle;
new $handleClass($exception);
} else {
self::error($exception);
}
}
}
exit;
exit(1);
});
}
/**
* 输出错误页面
*
* @access public
*
* @param mixed $exception 错误信息
*
* @return void
* @param Throwable $exception 错误信息
*/
public static function error($exception)
public static function error(Throwable $exception)
{
$isException = is_object($exception);
$message = '';
if ($isException) {
$code = $exception->getCode();
$message = $exception->getMessage();
} else {
$code = $exception;
}
$code = $exception->getCode() ?: 500;
$message = $exception->getMessage();
$charset = self::$charset;
if ($isException && $exception instanceof Typecho_Db_Exception) {
if ($exception instanceof Typecho_Db_Exception) {
$code = 500;
@error_log($message);
//覆盖原始错误信息
$message = 'Database Server Error';
@ -355,23 +330,8 @@ class Typecho_Common
} elseif ($exception instanceof Typecho_Db_Query_Exception) {
$message = 'Database Query Error';
}
} else {
switch ($code) {
case 500:
$message = 'Server Error';
break;
case 404:
$message = 'Page Not Found';
break;
default:
$code = 'Error';
break;
}
}
/** 设置http code */
if (is_numeric($code) && $code > 200) {
Typecho_Response::setStatus($code);
@ -428,7 +388,7 @@ class Typecho_Common
EOF;
}
exit;
exit(1);
}
/**

View File

@ -1,11 +1,4 @@
<?php
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id: Widget.php 107 2008-04-11 07:14:43Z magike.net $
*/
/**
* Typecho组件基类
@ -20,7 +13,7 @@ abstract class Typecho_Widget
* @access private
* @var array
*/
private static $_widgetPool = [];
private static $widgetPool = [];
/**
* widget别名
@ -28,7 +21,7 @@ abstract class Typecho_Widget
* @access private
* @var array
*/
private static $_widgetAlias = [];
private static $widgetAlias = [];
/**
* 数据堆栈
@ -118,7 +111,7 @@ abstract class Typecho_Widget
*/
public static function alias(string $widgetClass, string $aliasClass)
{
self::$_widgetAlias[$widgetClass] = $aliasClass;
self::$widgetAlias[$widgetClass] = $aliasClass;
}
/**
@ -134,17 +127,21 @@ abstract class Typecho_Widget
* @return Typecho_Widget
* @throws Typecho_Exception
*/
public static function widget(string $alias, $params = null, $request = null, bool $enableResponse = true): Typecho_Widget
{
public static function widget(
string $alias,
$params = null,
$request = null,
bool $enableResponse = true
): Typecho_Widget {
$parts = explode('@', $alias);
$className = $parts[0];
$alias = empty($parts[1]) ? $className : $parts[1];
if (isset(self::$_widgetAlias[$className])) {
$className = self::$_widgetAlias[$className];
if (isset(self::$widgetAlias[$className])) {
$className = self::$widgetAlias[$className];
}
if (!isset(self::$_widgetPool[$alias])) {
if (!isset(self::$widgetPool[$alias])) {
/** 如果类不存在 */
if (!class_exists($className)) {
throw new Typecho_Widget_Exception($className);
@ -166,10 +163,10 @@ abstract class Typecho_Widget
$widget = new $className($requestObject, $responseObject, $params);
$widget->execute();
self::$_widgetPool[$alias] = $widget;
self::$widgetPool[$alias] = $widget;
}
return self::$_widgetPool[$alias];
return self::$widgetPool[$alias];
}
/**
@ -190,8 +187,8 @@ abstract class Typecho_Widget
*/
public static function destroy(string $alias)
{
if (isset(self::$_widgetPool[$alias])) {
unset(self::$_widgetPool[$alias]);
if (isset(self::$widgetPool[$alias])) {
unset(self::$widgetPool[$alias]);
}
}
@ -243,8 +240,11 @@ abstract class Typecho_Widget
public function parse(string $format)
{
while ($this->next()) {
echo preg_replace_callback("/\{([_a-z0-9]+)\}/i",
[$this, '__parseCallback'], $format);
echo preg_replace_callback(
"/\{([_a-z0-9]+)\}/i",
[$this, '__parseCallback'],
$format
);
}
}

View File

@ -1380,7 +1380,7 @@ class Widget_Archive extends Widget_Abstract_Contents
/** 文件不存在 */
if (!$validated) {
Typecho_Common::error(500);
throw new Typecho_Widget_Exception(_t('文件不存在'), 500);
}
/** 挂接插件 */

View File

@ -1,12 +1,8 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id$
*/
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 执行模块
@ -21,38 +17,36 @@ class Widget_Do extends Typecho_Widget
* @access private
* @var array
*/
private $_map = [
'ajax' => 'Widget_Ajax',
'login' => 'Widget_Login',
'logout' => 'Widget_Logout',
'register' => 'Widget_Register',
'upgrade' => 'Widget_Upgrade',
'upload' => 'Widget_Upload',
'service' => 'Widget_Service',
'xmlrpc' => 'Widget_XmlRpc',
'comments-edit' => 'Widget_Comments_Edit',
'contents-page-edit' => 'Widget_Contents_Page_Edit',
'contents-post-edit' => 'Widget_Contents_Post_Edit',
private $map = [
'ajax' => 'Widget_Ajax',
'login' => 'Widget_Login',
'logout' => 'Widget_Logout',
'register' => 'Widget_Register',
'upgrade' => 'Widget_Upgrade',
'upload' => 'Widget_Upload',
'service' => 'Widget_Service',
'xmlrpc' => 'Widget_XmlRpc',
'comments-edit' => 'Widget_Comments_Edit',
'contents-page-edit' => 'Widget_Contents_Page_Edit',
'contents-post-edit' => 'Widget_Contents_Post_Edit',
'contents-attachment-edit' => 'Widget_Contents_Attachment_Edit',
'metas-category-edit' => 'Widget_Metas_Category_Edit',
'metas-tag-edit' => 'Widget_Metas_Tag_Edit',
'options-discussion' => 'Widget_Options_Discussion',
'options-general' => 'Widget_Options_General',
'options-permalink' => 'Widget_Options_Permalink',
'options-reading' => 'Widget_Options_Reading',
'plugins-edit' => 'Widget_Plugins_Edit',
'themes-edit' => 'Widget_Themes_Edit',
'users-edit' => 'Widget_Users_Edit',
'users-profile' => 'Widget_Users_Profile',
'backup' => 'Widget_Backup'
'metas-category-edit' => 'Widget_Metas_Category_Edit',
'metas-tag-edit' => 'Widget_Metas_Tag_Edit',
'options-discussion' => 'Widget_Options_Discussion',
'options-general' => 'Widget_Options_General',
'options-permalink' => 'Widget_Options_Permalink',
'options-reading' => 'Widget_Options_Reading',
'plugins-edit' => 'Widget_Plugins_Edit',
'themes-edit' => 'Widget_Themes_Edit',
'users-edit' => 'Widget_Users_Edit',
'users-profile' => 'Widget_Users_Profile',
'backup' => 'Widget_Backup'
];
/**
* 入口函数,初始化路由器
*
* @access public
* @return void
* @throws Typecho_Widget_Exception
* @throws Typecho_Widget_Exception|Typecho_Exception
*/
public function execute()
{
@ -69,7 +63,7 @@ class Widget_Do extends Typecho_Widget
}
} else {
/** 判断是否为plugin */
$actionTable = array_merge($this->_map, unserialize($this->widget('Widget_Options')->actionTable));
$actionTable = array_merge($this->map, unserialize($this->widget('Widget_Options')->actionTable));
if (isset($actionTable[$action])) {
$widgetName = $actionTable[$action];