framework icon indicating copy to clipboard operation
framework copied to clipboard

在系统输出和处理HttpEnd流程期间,没有捕捉异常,同时导致没有错误日志

Open augushong opened this issue 3 years ago • 2 comments

// 执行HTTP应用并响应
$http = (new App())->http;

$response = $http->run();

// 一下两个步骤中如果出现错误,则不会产生错误日志。
$response->send();

$http->end($response);

augushong avatar Apr 14 '22 03:04 augushong

比如,在控制器中直接返回一个对象,页面会报错,但这次报错不会写入日志。

augushong avatar Apr 14 '22 03:04 augushong

并且如果send方法中,如果getContent中出现报错,则导致设置的header失效,比如跨域中间件:

  /**
     * 发送数据到客户端
     * @access public
     * @return void
     * @throws \InvalidArgumentException
     */
    public function send(): void
    {

        // 处理输出数据
        $data = $this->getContent();   // 如果里面的内容出现报错,则会抛出异常,但是之前设置的header都不会执行,比如跨域中间件设置的header都不能用,并且不会产生日志。

        if (!headers_sent()) {
            if (!empty($this->header)) {
                // 发送状态码
                http_response_code($this->code);
                // 发送头部信息
                foreach ($this->header as $name => $val) {
                    header($name . (!is_null($val) ? ':' . $val : ''));
                }
            }

            if ($this->cookie) {
                $this->cookie->save();
            }
        }


        $this->sendData($data);

        if (function_exists('fastcgi_finish_request')) {
            // 提高页面响应
            fastcgi_finish_request();
        }
    }

augushong avatar Apr 14 '22 08:04 augushong