Skip to content

Commit 4a25636

Browse files
committed
update
1 parent 45c88e7 commit 4a25636

3 files changed

Lines changed: 140 additions & 3 deletions

File tree

src/RenderExceptionWithWhoops.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace think\Whoops;
6+
7+
use think\exception\Handle;
8+
use think\exception\HttpResponseException;
9+
use think\Response;
10+
use Throwable;
11+
use Whoops\Handler\PrettyPageHandler;
12+
13+
class RenderExceptionWithWhoops extends Handle
14+
{
15+
public function render($request, Throwable $e): Response
16+
{
17+
// Whoops 接管请求异常
18+
if (config('whoops.enable') && $this->app->isDebug()) {
19+
if ($e instanceof HttpResponseException) {
20+
return $e->getResponse();
21+
}
22+
23+
// 兼容 Cors Postman 请求
24+
// $request->isAjax() 判断不太正常
25+
if ($request->isJson() || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Postman') || (isset($_SERVER['HTTP_SEC_FETCH_MODE']) && $_SERVER['HTTP_SEC_FETCH_MODE'] === 'cors')) {
26+
return $this->handleAjaxException($e);
27+
}
28+
29+
$this->app->whoops->pushHandler(new PrettyPageHandler());
30+
return Response::create(
31+
$this->app->whoops->handleException($e),
32+
'html',
33+
$e->getCode()
34+
);
35+
}
36+
37+
// 其他错误交给系统处理
38+
return parent::render($request, $e);
39+
}
40+
41+
/**
42+
* 接管Ajax异常.
43+
*
44+
* @param Throwable $e
45+
*
46+
* @return void
47+
*/
48+
protected function handleAjaxException(Throwable $e)
49+
{
50+
$data = [
51+
'name' => get_class($e),
52+
'file' => $e->getFile(),
53+
'line' => $e->getLine(),
54+
'message' => $this->getMessage($e),
55+
'trace' => $e->getTrace(),
56+
'code' => $this->getCode($e),
57+
'source' => $this->getSourceCode($e),
58+
'datas' => $this->getExtendData($e),
59+
'tables' => [
60+
'GET Data' => $this->app->request->get(),
61+
'POST Data' => $this->app->request->post(),
62+
'Files' => $this->app->request->file(),
63+
'Cookies' => $this->app->request->cookie(),
64+
'Session' => $this->app->session->all(),
65+
'Server/Request Data' => $this->app->request->server(),
66+
'Environment Variables' => $this->app->request->env(),
67+
'ThinkPHP Constants' => $this->getConst(),
68+
],
69+
];
70+
71+
return Response::create($data, 'json', $e->getCode());
72+
}
73+
}

src/Whoops.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace think\Whoops;
4+
5+
use think\App;
6+
use think\facade\Env;
7+
use think\facade\Request;
8+
use Whoops\Handler\PrettyPageHandler;
9+
use Whoops\Run;
10+
11+
class Whoops
12+
{
13+
private $run;
14+
15+
private $options = [
16+
'editor' => '',
17+
'title' => '发生内部错误,请稍后再试',
18+
];
19+
20+
public function __construct(Run $run)
21+
{
22+
$this->run = $run;
23+
$this->options = array_merge($this->options, config('whoops'));
24+
}
25+
26+
public function __call($name, $arguments)
27+
{
28+
call_user_func_array([$this->run, $name], $arguments);
29+
}
30+
31+
public function handleException($exception)
32+
{
33+
$handlers = $this->run->getHandlers();
34+
35+
foreach ($handlers as $handler) {
36+
if ($handler instanceof PrettyPageHandler) {
37+
$handler->addDataTable('ThinkPHP Application', [
38+
'Version' => App::VERSION,
39+
'URI' => Request::url(true),
40+
'Request URI' => Request::url(),
41+
'Path Info' => Request::pathinfo(),
42+
'Query String' => Request::query() ?: '<none>',
43+
'HTTP Method' => Request::method(),
44+
'Base URL' => Request::baseUrl(),
45+
'Scheme' => Request::scheme(),
46+
'Port' => Request::port(),
47+
'Host' => Request::host(),
48+
]);
49+
50+
// 环境变量
51+
$_ENV = Env::get();
52+
53+
// 从异常堆栈跟踪中打开代码编辑器
54+
if ($this->options['editor']) {
55+
$handler->setEditor($this->options['editor']);
56+
}
57+
58+
// 设置标题
59+
$handler->setPageTitle($this->options['title']);
60+
}
61+
}
62+
63+
$this->run->handleException($exception);
64+
}
65+
}

src/WhoopsService.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace think\Whoops;
66

77
use think\Service;
8-
use think\Whoops\Hander\Whoops;
98

109
class WhoopsService extends Service
1110
{
1211
public function register()
1312
{
14-
$this->app->bind('whoops', Runner::class);
15-
$this->app->bind('think\exception\Handle', Whoops::class);
13+
$this->app->bind('whoops', Whoops::class);
14+
$this->app->bind('think\exception\Handle', RenderExceptionWithWhoops::class);
1615
}
1716
}

0 commit comments

Comments
 (0)