framework
framework copied to clipboard
中间件异常处理BUG
重现问题步骤如下: 定义中间件,代码如下:
namespace app\test\middleware;
use Closure;
use RuntimeException;
use think\Response;
class Test
{
public function handle($request, Closure $next)
{
throw new RuntimeException('测试middleware发生错误的情况1');
}
public function end(Response $response)
{
throw new RuntimeException('测试middleware发生错误的情况2');
}
}
在该代码中的handle、end方法各模拟抛出了一个错误,结果导致两个错误都输出到页面上。
预期结果应该是:
按照 handle -> end顺序,发现异常仅处理当前异常,进行一次输出,而不是多次输出。
框架设定如此不是bug,看官方文档,中间件的end在回调触发的时候请求响应输出已经完成了,end是在最后http触发end方法后调用的: https://github.com/top-think/framework/blob/2ce3a39725cb68fd559635920ac4e1fc9548822b/src/think/Http.php#L282
https://github.com/top-think/framework/blob/2ce3a39725cb68fd559635920ac4e1fc9548822b/src/think/Middleware.php#L157