最近公司线上项目经常报出服务器内部错误,也就是Yii2框架的An internal server error occurred
,经排查日志发现是某处业务逻辑抛出了一个Exception
拦截消息,但是消息没有捕获到,无法返回给前端,并且开发环境复现不出来。
最后在Yii2框架源码中找到了问题原因,文件vendor/yiisoft/yii2/base/ErrorHandler.php
中有这么一个方法来处理异常:
/**
* Converts an exception into a simple string.
* @param \Exception|\Error $exception the exception being converted
* @return string the string representation of the exception.
*/
public static function convertExceptionToString($exception)
{
if ($exception instanceof UserException) {
return "{$exception->getName()}: {$exception->getMessage()}";
}
if (YII_DEBUG) {
return static::convertExceptionToVerboseString($exception);
}
return 'An internal server error occurred.';
}
如果没有使用UserException
来抛出异常,并且关闭了debug,框架将会把异常消息统一封装成An internal server error occurred
,导致前端拿不到真正的异常消息。解决方式很简单,所有需要返回给前端的异常,全部使用UserException
来抛出。
Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas