|
4 | 4 |
|
5 | 5 | namespace think\Whoops\Hander; |
6 | 6 |
|
7 | | -use think\App; |
8 | | -use think\exception\Handle; |
9 | | -use think\exception\HttpResponseException; |
10 | 7 | use think\Response; |
11 | | -use think\Whoops\Runner; |
| 8 | +use think\exception\Handle; |
12 | 9 | use Throwable; |
13 | | -use Whoops\Handler\JsonResponseHandler; |
14 | 10 | use Whoops\Handler\PrettyPageHandler; |
| 11 | +use think\exception\HttpResponseException; |
15 | 12 |
|
16 | 13 | class Whoops extends Handle |
17 | 14 | { |
18 | | - private $runner; |
19 | | - |
20 | | - public function __construct(App $app, Runner $runner) |
21 | | - { |
22 | | - parent::__construct($app); |
23 | | - $this->runner = $runner; |
24 | | - } |
25 | | - |
26 | 15 | public function render($request, Throwable $e): Response |
27 | 16 | { |
28 | 17 | // Whoops 接管请求异常 |
29 | | - if (config('whoops.enable') && $this->app->isDebug()) { |
| 18 | + if (config('whoops.enable')) { |
30 | 19 | if ($e instanceof HttpResponseException) { |
31 | 20 | return $e->getResponse(); |
32 | 21 | } |
33 | 22 |
|
34 | | - $this->runner->pushHandler(new PrettyPageHandler()); |
35 | | - |
36 | 23 | // 兼容 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); |
41 | 30 | } |
42 | | - |
43 | | - $this->runner->register(); |
44 | | - |
45 | | - $this->runner->handleException($e); |
46 | 31 | } |
47 | 32 |
|
48 | 33 | // 其他错误交给系统处理 |
49 | 34 | return parent::render($request, $e); |
50 | 35 | } |
| 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 | + } |
51 | 68 | } |
0 commit comments