的参数1必须是Exception的实例

的参数1必须是Exception的实例

本文介绍了laravel:传递给App \ Exceptions \ CustomException :: report()的参数1必须是Exception的实例,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5.2中创建了一个自定义异常类.直到laravel 5.4之前,它都能正常工作.

I have created a custom exception class in Laravel 5.2. It works well till laravel 5.4.

当我试图在laravel 5.5中使用相同的自定义异常类时,它会引发以下错误.

When Im trying to use the same custom exception class with laravel 5.5 it is throwing following error.

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)

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

Here is the custom exception class I've been using

<?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

任何人都可以解释一下为什么它抛出参数必须是异常实例吗?任何帮助将不胜感激.

Could anybody will explain me why it is throwing argument must be instance of exception ? Any help would be greatly appreciated.

我的编程环境PHP 7.0.1 Laravel 5.5

My programming environmentPHP 7.0.1Laravel 5.5

推荐答案

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

The exception handler in Laravel 5.5 checks if the exception has a report method, and if so, let the exception handle the reporting itself. This means that the handler will see your report method, and call $e->report();, but your report method requires a parameter.

这是在 Handler :: report .

如果要使用此功能,则需要在报告方法中删除该参数(该参数应为报告本身; $this),或者如果不想让Laravel调用它,则重命名该方法(并失败) ).

You either need to remove the parameter in your report method (it should be reporting itself; $this) if you want to use this functionality, or rename the method if you don't want Laravel to call it (and fail).

相关: Laravel 5.5添加了对自定义异常报告的支持

这篇关于laravel:传递给App \ Exceptions \ CustomException :: report()的参数1必须是Exception的实例,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:05