广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

下面thinkphp框架教程栏目将给大家介绍关于TP5自定义全局异常处理提示render不兼容问题,希望对需要的朋友有所帮助!
TP5自定义全局异常处理,所有抛出的异常都通过自定义render方法渲染,再返回客户端显示。需要自定义handle的render方法并覆盖:
namespace app\lib\exception; use think\Exception; use think\exception\Handle;class ExceptionHandler extends Handle { public function render(Exception $e) { //TODO: return json('invalid request') } }登录后复制之后出现postman检验接口出现如下错误提示不兼容:
追踪到原始的Handle.php文件,
查看下use,发现源文件用的是Exception,而我用的think\Exception:
修改下代码:
namespace app\lib\exception; use Exception; use think\exception\Handle;class ExceptionHandler extends Handle { public function render(Exception $e) { //TODO: return json('invalid request') } }登录后复制结果正确啦:


发表评论