自定义 thinkphp5.0全局异常报错,继承基类\exception解决

 

情景介绍:url请求是get方式,此次报错是测试post请求,异常返回错误信息是否正确,结果异常返回信息有误,返回信息不正确。

以下下报错信息,显示  自定义全局异常报错信息是传入HttpException参数给app\lib\exception\ExceptionHandle::render() 方法(本应传入think\Exception类型的参数),结果异常信息不能自动转换成正确的信息,查看think\Exception 和 HttpException 两个class类,都同时继承了\exception基类,也就是think\Exception 和 HttpException 两个class类并没有继承关系,二者互不关联,所以参数类型不能识别,将app\lib\exception\ExceptionHandle::render()的参数改为以基类的方式参数传入,就可解决此问题。

think\exception\ErrorException' with message 'Argument 1 passed to app\lib\exception\ExceptionHandle::render() must be an instance of think\Exception

 think\exception\ErrorException: Argument 1 passed to app\lib\exception\ExceptionHandle::render() must be an instance of think\Exception, instance of think\exception\HttpException given,

自定义 thinkphp5.0全局异常报错,继承基类\exception解决_第1张图片

( ! ) Fatal error: Uncaught exception 'think\exception\ErrorException' with message 'Argument 1 passed to app\lib\exception\ExceptionHandle::render() must be an instance of think\Exception, instance of think\exception\HttpException given, called in D:\phpStudy\PHPTutorial\WWW\humour\thinkphp\library\think\Error.php on line 47 and defined' in D:\phpStudy\PHPTutorial\WWW\humour\application\lib\exception\ExceptionHandle.php on line 25
( ! ) think\exception\ErrorException: Argument 1 passed to app\lib\exception\ExceptionHandle::render() must be an instance of think\Exception, instance of think\exception\HttpException given, called in D:\phpStudy\PHPTutorial\WWW\humour\thinkphp\library\think\Error.php on line 47 and defined in D:\phpStudy\PHPTutorial\WWW\humour\application\lib\exception\ExceptionHandle.php on line 25
Call Stack
# Time Memory Function Location
1 0.4658 1188352 think\Error::appException( ) ...\Error.php:0
2 0.4697 1241032 app\lib\exception\ExceptionHandle->render( ) ...\Error.php:47
3 0.4697 1241768 think\Error::appError( ) ...\Error.php:25

[1] ErrorException in ExceptionHandle.php line 25

Uncaught exception 'think\exception\ErrorException' with message 'Argument 1 passed to app\lib\exception\ExceptionHandle::render() must be an instance of think\Exception, instance of think\exception\HttpException given, called in D:\phpStudy\PHPTutorial\WWW\humour\thinkphp\library\think\Error.php on line 47 and defined' in D:\phpStudy\PHPTutorial\WWW\humour\application\lib\exception\ExceptionHandle.php:25
Stack trace:
#0 D:\phpStudy\PHPTutorial\WWW\humour\application\lib\exception\ExceptionHandle.php(25): think\Error::appError(4096, 'Argument 1 pass...', 'D:\\phpStudy\\PHP...', 25, Array)
#1 D:\phpStudy\PHPTutorial\WWW\humour\thinkphp\library\think\Error.php(47): app\lib\exception\ExceptionHandle->render(Object(think\exception\HttpException))
#2 [internal function]: think\Error::appException(Object(think\exception\HttpException))
#3 {main}
thrown



 
  1. class ExceptionHandle extends Handle
  2. {
  3. private $code;
  4. private $msg;
  5. private $errorCode;
  6. // 需要返回客户端当前的请求URL
  7. public function render(Exception $e)
  8. {
  9. if ($e instanceof BaseException) {
  10. // 如果是自定义异常
  11. $this->code = $e->code;
  12. $this->msg = $e->msg;
  13. $this->errorCode = $e->errorCode;
  14. } else {
  15. if(config('app_debug')){
  16. return parent::render($e);

Call Stack

  1. in ExceptionHandle.php line 25
  2. at Error::appShutdown()

你可能感兴趣的:(framework)