Ingen beskrivning
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.

ExceptionHandle.php 975B

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