Laravel 5 Handler.php line 38 异常错误解决
前言
本文主要介绍如何解决 Laravel 5 中出现的 FatalErrorException in Handler.php line 38 异常错误,分享相关排查思路与修复代码,供开发者参考学习。
1、错误提示
FatalErrorException in Handler.php line 38:
Uncaught TypeError: Argument 1 passed to App\Exceptions\Handler::report() must be an instance of Exception, instance of Error given, called in D:\www\activity\vendor\compiled.php on line 1817 and defined in D:\www\activity\app\Exceptions\Handler.php:38
Stack trace:
#0 D:\www\activity\vendor\compiled.php(1817): App\Exceptions\Handler->report(Object(Error))
#1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Error))
#2 {main}
thrown
错误根源在于 D:\www\activity\vendor\compiled.php 第 1817 行传递的变量 $e 并非 Exception 类的实例对象(此解释基于对原始错误日志的分析)。
2、解决方案
在报错位置增加对变量 $e 的类型检查,若其不属于 Exception 类型,则手动创建一个新的异常实例进行包装。
if (!$e instanceof \Exception) {
$e = new FatalThrowableError($e);
}
修改后的代码逻辑如下:
public function handleException($e)
{
if (!$e instanceof \Exception) {
$e = new FatalThrowableError($e);
}
$this->getExceptionHandler()->report($e);
if ($this->app->runningInConsole()) {
$this->renderForConsole($e);
} else {
$this->renderHttpResponse($e);
}
}
总结
以上就是本文的全部内容,希望上述内容对大家的学习或工作有所帮助。如有任何疑问,欢迎留言交流,感谢支持。