fix err display

This commit is contained in:
Default
2025-04-29 17:07:26 +08:00
parent 0137ecaa69
commit 9862a1b96a
5 changed files with 165 additions and 2 deletions

View File

@@ -0,0 +1,66 @@
<?php
namespace app\home;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
/**
* 应用异常处理类
*/
class HomeExceptionHandle extends Handle
{
/**
* 不需要记录信息(日志)的异常类列表
* @var array
*/
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
];
/**
* 记录异常信息(包括日志或者其它方式记录)
*
* @access public
* @param Throwable $exception
* @return void
*/
public function report(Throwable $exception): void
{
// 使用内置的方式记录异常日志
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
if ($e instanceof HttpException && $e->getStatusCode() == 404) {
return view('../public/error', [
'strErrMsg' => $e->getMessage(),
'strErrCode' => $e->getStatusCode()
])->code(404);
}
// 其他错误交给系统处理
return parent::render($request, $e);
}
}

View File

@@ -0,0 +1,9 @@
<?php
use app\home\HomeExceptionHandle;
// 容器Provider定义文件
return [
'think\exception\Handle' => HomeExceptionHandle::class,
];

View File

@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 - 页面未找到</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f2f2f2;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #333;
}
.container {
max-width: 500px;
padding: 20px;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
color: #e74c3c;
}
p {
font-size: 16px;
margin-bottom: 10px;
}
a {
color: #3498db;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.countdown {
font-weight: bold;
color: #e74c3c;
}
</style>
<script>
let countdown = 5;
function startCountdown() {
const countdownElement = document.getElementById('countdown');
const interval = setInterval(() => {
countdown--;
countdownElement.textContent = countdown;
if (countdown <= 0) {
clearInterval(interval);
window.history.back();
}
}, 1000);
}
</script>
</head>
<body onload="startCountdown()">
<div class="container">
<h1>{$strErrCode}</h1>
<p>{$strErrMsg}</p>
<p>5 秒后将自动返回上一页。</p>
<p>倒计时:<span id="countdown" class="countdown">5</span></p>
<p>或者 <a href="/">点击这里返回首页</a></p>
</div>
</body>
</html>