init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace app\bootstrap;
|
||||
|
||||
use app\utils\Logger;
|
||||
use Throwable;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
|
||||
class Handler extends \support\exception\Handler
|
||||
{
|
||||
public function render(Request $request, Throwable $exception): Response
|
||||
{
|
||||
Logger::error($exception->getMessage(),$exception);
|
||||
$code = $exception->getCode();
|
||||
$debug = $this->_debug ?? $this->debug;
|
||||
if ($request->expectsJson()) {
|
||||
$json = ['code' => $code ?: 500, 'msg' => $debug ? $exception->getMessage() : 'Server internal error', 'type' => 'failed'];
|
||||
$debug && $json['traces'] = (string)$exception;
|
||||
return new Response(200, ['Content-Type' => 'application/json'],
|
||||
\json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
$error = $debug ? \nl2br((string)$exception) : 'Server internal error';
|
||||
return new Response(500, [], $error);
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,9 @@ class SqlDebug implements Bootstrap
|
||||
}
|
||||
|
||||
$appPath = app_path();
|
||||
$pluginPath = dirname($appPath) . DIRECTORY_SEPARATOR . 'plugin';
|
||||
|
||||
if (Config::getInstance()->dbDebug) Db::connection()->listen(function (QueryExecuted $queryExecuted) use ($appPath) {
|
||||
if (Config::getInstance()->dbDebug) Db::connection()->listen(function (QueryExecuted $queryExecuted) use ($pluginPath, $appPath) {
|
||||
// 过滤掉 "select 1" 这类心跳检测SQL
|
||||
if (isset($queryExecuted->sql) && $queryExecuted->sql !== "select 1") {
|
||||
$bindings = $queryExecuted->bindings;
|
||||
@@ -60,7 +61,7 @@ class SqlDebug implements Bootstrap
|
||||
// 定位产生SQL的业务文件/行号/方法
|
||||
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
foreach ($traces as $trace) {
|
||||
if (isset($trace['file'], $trace['function']) && str_contains($trace['file'], $appPath)) {
|
||||
if (isset($trace['file'], $trace['function']) && (str_contains($trace['file'], $appPath) || str_contains($trace['file'], $pluginPath))) {
|
||||
// 格式化文件路径(去掉项目根目录,只保留相对路径)
|
||||
$file = str_replace(base_path(), '', $trace['file']);
|
||||
$file = ltrim($file, '/\\');
|
||||
|
||||
Reference in New Issue
Block a user