ExceptionHandle.php 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. {
  15. $statuscode = $code = 500;
  16. $msg = 'An error occurred';
  17. // 验证异常
  18. if ($e instanceof \think\exception\ValidateException)
  19. {
  20. $code = 0;
  21. $statuscode = 200;
  22. $msg = $e->getError();
  23. }
  24. // Http异常
  25. if ($e instanceof \think\exception\HttpException)
  26. {
  27. $statuscode = $code = $e->getStatusCode();
  28. }
  29. return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
  30. }
  31. //其它此交由系统处理
  32. return parent::render($e);
  33. }
  34. }