【问题标题】:PHP Exceptions CodeIgniterPHP 异常 CodeIgniter
【发布时间】:2017-11-09 14:11:18
【问题描述】:

我的代码生成 ssse 以下错误:

致命错误:未捕获的类型错误:参数 1 传递给 CI_Exceptions::show_exception() 必须是 Exception 的一个实例, 给定的错误实例,调用 C:\xampp\htdocs\gog\lib\core\Common.php 在第 662 行并在 C:\xampp\htdocs\gog\lib\core\Exceptions.php:190 堆栈跟踪:#0 C:\xampp\htdocs\gog\lib\core\Common.php(662): CI_Exceptions->show_exception(Object(Error)) #1 [内部函数]: _exception_handler(Object(Error)) #2 {main} 在第 190 行的 C:\xampp\htdocs\gog\lib\core\Exceptions.php 中抛出

描述的代码行如下:

    function _exception_handler(Throwable $exception)
{
    $_error =& load_class('Exceptions', 'core');
    $_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine());

    // Should we display the error?
    if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
    {
        $_error->show_exception($exception);   //line 662

    }

    exit(1); // EXIT_ERROR
}

public function show_exception(Exception $exception)  //line 190
    {
        $templates_path = config_item('error_views_path');
        if (empty($templates_path))
        {
            $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
        }

        $message = $exception->getMessage();
        if (empty($message))
        {
            $message = '(null)';
        }

        if (is_cli())
        {
            $templates_path .= 'cli'.DIRECTORY_SEPARATOR;
        }
        else
        {
            set_status_header(500);
            $templates_path .= 'html'.DIRECTORY_SEPARATOR;
        }

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush();
        }

        ob_start();
        include($templates_path.'error_exception.php');
        $buffer = ob_get_contents();
        ob_end_clean();
        echo $buffer;
    }

谁能指出我的问题?

【问题讨论】:

  • $exception 变量不是Exception 的实例(看到你的声明,它是Throwable 的实例

标签: php codeigniter exception exception-handling


【解决方案1】:

没有任何 CodeIgniter 版本具有 _exception_handler() 签名。当您修改库存框架文件时会发生这种情况。

下载最新的 CodeIgniter 的新副本并用它替换您的。

【讨论】:

    【解决方案2】:

    您从_exception_handler(Throwable $exception) 内部调用show_exception(Exception $exception)。由于前者将 Exception 作为参数,因此您不能像在第 662 行中那样给它一个 Throwable。Exception 实现了 Throwable 接口,但这并不能保证所有 Throwable 都是 Exceptions(例如,它们可以是 @987654324 类型@)。

    _exception_handler(Throwable $exception) 替换为_exception_handler(Exception $exception),或将show_exception(Exception $exception) 更改为show_exception(Throwable $exception),并根据需要相应地更改方法主体。

    【讨论】:

    • 此外,如果参数名称是 Throwable 类型,您可能希望将参数名称从 exception 更改为 throwable,因为这提高了可读性,并且不会让读者混淆其类型与预期不同.
    • 有问题的代码是 CodeIgniter 本身的一部分。 OP 不应该碰它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2019-04-14
    • 1970-01-01
    • 2012-07-22
    • 2013-01-12
    • 2011-01-20
    相关资源
    最近更新 更多