You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

38 lines
957 B

  1. <?php
  2. namespace app\api\library;
  3. use Exception;
  4. use think\exception\Handle;
  5. /**
  6. * 自定义API模块的错误显示
  7. */
  8. class ExceptionHandle extends Handle
  9. {
  10. public function render(Exception $e)
  11. {
  12. // 在生产环境下返回code信息
  13. if (!\think\Config::get('app_debug')) {
  14. $statuscode = $code = 500;
  15. $msg = 'An error occurred';
  16. // 验证异常
  17. if ($e instanceof \think\exception\ValidateException) {
  18. $code = 0;
  19. $statuscode = 200;
  20. $msg = $e->getError();
  21. }
  22. // Http异常
  23. if ($e instanceof \think\exception\HttpException) {
  24. $statuscode = $code = $e->getStatusCode();
  25. }
  26. return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
  27. }
  28. //其它此交由系统处理
  29. return parent::render($e);
  30. }
  31. }