tp 配合 uniapp开发移动端,因为tp 默认报错页面为html,但在调试uniapp上调试不能直接看见报错原因。为了方便调试时,能直接看见报错,将tp 报错返回转为json格式。
- 在config/app.php 中直接修改( 需要其它返回异常错误,可以参考 Handle.php)
// 异常处理handle类 留空使用 thinkphp\library\think\exception\Handle
//'exception_handle' => '',
'exception_handle' => function($e) {
// 异常报错
return json([
'msg'=>'报错:'.$e->getMessage(),
'api_url'=> $_SERVER['REQUEST_URI'],
'code'=>'500',
'file_line'=> $e->getFile() . ' '.$e->getLine().'行',
]);
},
- 效果
{
"msg":"报错:controller not exists:app\index\controller\test",
"api_url2":"\api\public\index\test",
"code":"500",
"file_line":"xxx\thinkphp\library\think\route\dispatch\Module.php 96行"
}
- 仅开发调试使用