Skip to content

Commit cd90e16

Browse files
committed
ajax请求显示详细信息
1 parent 0c0ac99 commit cd90e16

4 files changed

Lines changed: 43 additions & 25 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"think\\Whoops\\WhoopsService"
3131
],
3232
"config": {
33-
"whoops": "src/Config/config.php"
33+
"whoops": "config/config.php"
3434
}
3535
}
3636
}
File renamed without changes.

src/Hander/Whoops.php

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,65 @@
44

55
namespace think\Whoops\Hander;
66

7-
use think\App;
8-
use think\exception\Handle;
9-
use think\exception\HttpResponseException;
107
use think\Response;
11-
use think\Whoops\Runner;
8+
use think\exception\Handle;
129
use Throwable;
13-
use Whoops\Handler\JsonResponseHandler;
1410
use Whoops\Handler\PrettyPageHandler;
11+
use think\exception\HttpResponseException;
1512

1613
class Whoops extends Handle
1714
{
18-
private $runner;
19-
20-
public function __construct(App $app, Runner $runner)
21-
{
22-
parent::__construct($app);
23-
$this->runner = $runner;
24-
}
25-
2615
public function render($request, Throwable $e): Response
2716
{
2817
// Whoops 接管请求异常
29-
if (config('whoops.enable') && $this->app->isDebug()) {
18+
if (config('whoops.enable')) {
3019
if ($e instanceof HttpResponseException) {
3120
return $e->getResponse();
3221
}
3322

34-
$this->runner->pushHandler(new PrettyPageHandler());
35-
3623
// 兼容 Cors Postman 请求
37-
if ($request->isAjax() ||
38-
false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Postman') ||
39-
(isset($_SERVER['HTTP_SEC_FETCH_MODE']) && $_SERVER['HTTP_SEC_FETCH_MODE'] === 'cors')) {
40-
$this->runner->pushHandler(new JsonResponseHandler());
24+
if ($request->isAjax() || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Postman') || (isset($_SERVER['HTTP_SEC_FETCH_MODE']) && $_SERVER['HTTP_SEC_FETCH_MODE'] === 'cors')) {
25+
return $this->handleAjaxException($e);
26+
} else {
27+
$this->app->whoops->pushHandler(new PrettyPageHandler());
28+
$this->app->whoops->register();
29+
$this->app->whoops->handleException($e);
4130
}
42-
43-
$this->runner->register();
44-
45-
$this->runner->handleException($e);
4631
}
4732

4833
// 其他错误交给系统处理
4934
return parent::render($request, $e);
5035
}
36+
37+
/**
38+
* 接管Ajax异常
39+
*
40+
* @param Throwable $e
41+
* @return void
42+
*/
43+
protected function handleAjaxException(Throwable $e)
44+
{
45+
$data = [
46+
'name' => get_class($e),
47+
'file' => $e->getFile(),
48+
'line' => $e->getLine(),
49+
'message' => $this->getMessage($e),
50+
'trace' => $e->getTrace(),
51+
'code' => $this->getCode($e),
52+
'source' => $this->getSourceCode($e),
53+
'datas' => $this->getExtendData($e),
54+
'tables' => [
55+
'GET Data' => $this->app->request->get(),
56+
'POST Data' => $this->app->request->post(),
57+
'Files' => $this->app->request->file(),
58+
'Cookies' => $this->app->request->cookie(),
59+
'Session' => $this->app->session->all(),
60+
'Server/Request Data' => $this->app->request->server(),
61+
'Environment Variables' => $this->app->request->env(),
62+
'ThinkPHP Constants' => $this->getConst(),
63+
],
64+
];
65+
66+
return Response::create($data, 'json', $e->getCode());
67+
}
5168
}

src/WhoopsService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class WhoopsService extends Service
1111
{
1212
public function register()
1313
{
14+
$this->app->bind('whoops', Runner::class);
1415
$this->app->bind('think\exception\Handle', Whoops::class);
1516
}
1617
}

0 commit comments

Comments
 (0)