top-think / framework

ThinkPHP Framework
http://www.thinkphp.cn
Other
2.71k stars 1.05k forks source link

框架加载配置文件期间的异常无法捕获 #2999

Open ichynul opened 2 months ago

ichynul commented 2 months ago

所属功能组件

异常(Exception)

ThinkPHP 版本

6.1

操作系统

linux

错误信息

复现。 在config目录创建文件test.php,运行后页面空白,无报错也无错误日志。

<?php

use Aa\Bb;

return [
    'test' => Bb::Cc
];

其它说明

这个问题Aa\Bb这个类不存在引起的,类似的很多问题都会有同样结果。 app/AppService.php provider.php service.php 这些文件中的代码错误都会引起。

问题是 Http->initialize() 没有捕获异常,程序提前结束,没走到异常渲染和日志记录。

以下是简单的解决办法。 页面会输出:test.php#6:Class 'Aa\Bb' not found,起码能起到错误定位的作用。 至于说渲染错误页面,好像有点复杂。

//`Http.php`
/**
     * 执行应用程序
     * @access public
     * @param Request|null $request
     * @return Response
     */
    public function run(Request $request = null): Response
    {
        //初始化
        try {
            $this->initialize();
        } catch (Throwable $e) {
            exit(basename($e->getFile()) . '#' . $e->getLine() . ':' . $e->getMessage());
        }

        //自动创建request对象
        $request = $request ?? $this->app->make('request', [], true);
        $this->app->instance('request', $request);

        try {
            $response = $this->runWithRequest($request);
        } catch (Throwable $e) {
            $this->reportException($e);

            $response = $this->renderException($request, $e);
        }

        return $response;
    }

至于我呢,就是移除了yansongda/pay这个包,但他有个配置文件config/pay.php没移除。 配置里面有Yansongda\Pay\Pay::MODE_NORMAL这个东西。 页面直接空白,找了半天才定位到问题。。。因为我同时移除了好几个包,我只能一个一个的测试是哪个包引起的。