laravel:传递给App \ Exceptions \ CustomException :: report()的参数1必须是Exception的一个实例,

时间:2017-10-01 19:24:46

标签: laravel php-7.1 laravel-5.5 laravel-exceptions

我在Laravel 5.2中创建了一个自定义异常类。它一直很好,直到laravel 5.4。

当我尝试在laravel 5.5中使用相同的自定义异常类时,它会抛出以下错误。

Type error: Argument 1 passed to App\Utility\Exceptions\CustomException::report() must be an instance of Exception, none given, called in /var/www/html/bubbles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php on line 102 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 1 passed to App\\Utility\\Exceptions\\CustomException::report() must be an instance of Exception, none given, called in /var/www/html/bubbles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php on line 102 at /var/www/html/bubbles/app/Utility/Exceptions/CustomException.php:39)

这是我一直在使用的自定义异常类

<?php 

namespace App\Utility\Exceptions;

use Illuminate\Support\Facades\Lang;
use Exception;


class CustomException extends Exception  
{

    private $error_code = NULL;

    private $error_info = NULL;

    function __construct($type = NULL, $errors = NULL)
    {
        $this->error_code = $type['error_code'];
        $this->error_info = $errors;

        $message = Lang::get('exceptions.'.$this->error_code);

        parent::__construct($message, $type['code'], NULL);
    }


    public function report(Exception $exception)
    {
        parent::report($exception);
    }


    public function getErrorCode()
    {
        return $this->error_code;
    }


    public function getErrorInfo()
    {
        return $this->error_info;
    }
 }
 // end of class CustomException
 // end of file CustomException.php

有人会解释为什么抛出参数必须是异常的实例吗?任何帮助将不胜感激。

我的编程环境 PHP 7.0.1 Laravel 5.5

1 个答案:

答案 0 :(得分:1)

Laravel 5.5中的异常处理程序检查异常是否有报告方法,如果是,则让异常处理报告本身。这意味着处理程序会看到您的报告方法,并调用$e->report();,但您的报告方法需要一个参数。

这是在Handler::report中完成的。

如果您想要使用此功能,您需要删除报告方法中的参数(它应该报告自己; $this),如果您不想让Laravel调用,则需要重命名该方法它(并失败)。

相关:Laravel 5.5 Adds Support for Custom Exception Reporting