【问题标题】:laravel: Argument 1 passed to App\Exceptions\CustomException::report() must be an instance of Exception,laravel:传递给 App\Exceptions\CustomException::report() 的参数 1 必须是 Exception 的实例,
【发布时间】:2018-03-12 23:10:37
【问题描述】:

我在 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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    相关:Laravel 5.5 Adds Support for Custom Exception Reporting

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 2018-01-09
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多