Skip to content

Commit 0316968

Browse files
committed
feat: 优化错误信息
1 parent 26d39d0 commit 0316968

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

library/app/app.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ public static function boot($url)
1414
require APP_MODULES . 'route.php';
1515
}
1616
// 回退路由
17-
$router->map('GET', '*', function ($args) {
18-
$model = new ErrorModel($args);
19-
$model->warning('not found', $args);
20-
return $model;
17+
$router->map('GET', '*', function () {
18+
header('HTTP/1.1 404 Not Found');
19+
AppHelper::message('请求的页面不存在', 'info');
2120
});
2221
// 匹配路由
2322
$request = $router->match($url ?: '/');

library/app/app_helper.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,50 @@ class AppHelper
44
{
55
/**
66
* 读写缓存数据
7-
* @param string $name
7+
* @param string $file
88
* @param mixed $data
99
* @return mixed
1010
*/
1111
public static function storage($file, $data = true)
1212
{
1313
//读取数据存储
1414
if ($data === true) {
15-
return is_file($file) ? include($file) : array();
15+
return is_file($file) ? include($file) : [];
1616
}
1717
//读取有效数据
1818
if (is_numeric($data)) {
1919
$time = is_file($file) ? filemtime($file) : 0;
2020
return $time > $data ? self::storage($name) : false;
2121
}
2222
//写入数据存储
23+
$sdir = dirname($file);
2324
$data = var_export($data, true);
24-
is_dir(dirname($file)) || mkdir(dirname($file), 0755, true);
25+
is_dir($sdir) || mkdir($sdir, 0755, true);
2526
return file_put_contents($file, "<?php\nreturn {$data};\n");
2627
}
28+
29+
/**
30+
* 输出提示信息
31+
* @param string|array $text
32+
* @param string $type
33+
* @return void
34+
*/
35+
public static function message($text, $type = 'info')
36+
{
37+
echo '<html>';
38+
echo '<meta charset="utf-8" />';
39+
echo '<title>提示消息</title>';
40+
echo '<meta content="width=device-width,initial-scale=1.0" name="viewport" />';
41+
echo '<link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet" />';
42+
echo '<body>';
43+
echo '<div class="container p-4">';
44+
$text = is_array($text) ? $text : [$text];
45+
array_walk($text, function ($text) use ($type) {
46+
echo "<div class=\"alert alert-{$type}\">{$text}</div>";
47+
});
48+
echo '</div>';
49+
echo '</body>';
50+
echo '</html>';
51+
exit;
52+
}
2753
}

library/app/app_router.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AppRouter
2626
'h' => '[0-9A-Fa-f]++',
2727
'*' => '.+?',
2828
'**' => '.++',
29-
'' => '[^/\.]++'
29+
'' => '[^/\.]++',
3030
];
3131

3232
/**
@@ -195,7 +195,7 @@ public function match($requestUrl = null, $requestMethod = null)
195195
foreach ($this->routes as $handler) {
196196
list($methods, $route, $target, $name) = $handler;
197197

198-
$method_match = (stripos($methods, $requestMethod) !== false);
198+
$method_match = stripos($methods, $requestMethod) !== false;
199199

200200
// Method did not match, continue to next route.
201201
if (!$method_match) {
@@ -218,7 +218,6 @@ public function match($requestUrl = null, $requestMethod = null)
218218
if (strncmp($requestUrl, $route, $position) !== 0 && ($lastRequestUrlChar === '/' || $route[$position - 1] !== '/')) {
219219
continue;
220220
}
221-
222221
$regex = $this->compileRoute($route);
223222
$match = preg_match($regex, $requestUrl, $params) === 1;
224223
}

library/autoload.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@
4242
return true;
4343
}
4444
});
45+
46+
// 注册错误处理函数
47+
48+
set_error_handler(function ($no, $str, $file, $line) {
49+
AppHelper::message("{$str} in {$file} on line {$line}", 'danger');
50+
});
51+
52+
// 注册异常处理函数
53+
54+
set_exception_handler(function ($e) {
55+
AppHelper::message($e->getMessage(), 'warning');
56+
});

0 commit comments

Comments
 (0)