我在 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
有人能解释一下为什么它抛出的参数必须是异常实例吗?任何帮助将不胜感激。
我的编程环境
PHP7.0.1拉维尔5.5
Laravel 5.5 中的异常处理程序检查异常是否有报告方法,如果有,则让异常自行处理报告。这意味着处理程序将看到您的报告方法,并调用$e->report();
,但是您的报告方法需要一个参数。
这是在处理程序::报告 https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Exceptions/Handler.php#L101-L103.
您需要删除报告方法中的参数(它应该是报告本身;$this
) 如果您想使用此功能,或者如果您不希望 Laravel 调用它(并失败),则重命名该方法。
相关的:Laravel 5.5 添加了对自定义异常报告的支持 https://laravel-news.com/custom-exception-reporting
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)