laravel: Argument 1 passed to App\Exceptions\CustomException::report() must be an instance of Exception,

允我心安 提交于 2019-12-11 03:47:20

问题


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

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.

My programming environment PHP 7.0.1 Laravel 5.5


回答1:


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.

This is done in Handler::report.

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).

Relevant: Laravel 5.5 Adds Support for Custom Exception Reporting



来源:https://stackoverflow.com/questions/46516045/laravel-argument-1-passed-to-app-exceptions-customexceptionreport-must-be-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!