Files
zyt/identity-admin/server/app/ExceptionHandle.php
T
2026-11-11 13:53:14 +08:00

32 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace app;
use app\exception\ApiError;
use think\exception\Handle;
use think\exception\HttpException;
use think\Response;
use Throwable;
final class ExceptionHandle extends Handle
{
public function render($request, Throwable $error): Response
{
if ($error instanceof ApiError) {
$status = $error->status;
$message = $error->getMessage();
} elseif ($error instanceof HttpException) {
$status = $error->getStatusCode();
$message = $status === 404 ? '接口不存在' : '请求无法处理';
} elseif ($error instanceof \PDOException && str_starts_with((string) $error->getCode(), '23')) {
$status = 409;
$message = '记录冲突或仍有关联数据';
} else {
$status = 500;
$message = '服务暂时不可用';
}
return json(['code' => 0, 'msg' => $message, 'data' => (object) []], $status)
->header(['Cache-Control' => 'no-store', 'X-Content-Type-Options' => 'nosniff']);
}
}