ApiException.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace app\ebapi\controller;
  3. use app\core\util\ApiLogs;
  4. use Exception;
  5. use service\JsonService;
  6. use think\exception\Handle;
  7. use think\exception\ValidateException;
  8. use think\exception\PDOException;
  9. use think\exception\ErrorException;
  10. use think\exception\HttpException;
  11. use think\exception\DbException;
  12. use app\core\lib\BaseException;
  13. class ApiException extends Handle
  14. {
  15. public function render(Exception $e){
  16. //记录错误日志
  17. if ($e instanceof \think\Exception) ApiLogs::recodeErrorLog($e);
  18. //可以在此交由系统处理
  19. if(\think\Config::get('app_debug')) return Handle::render($e);
  20. //参数验证错误
  21. if ($e instanceof ValidateException) return JsonService::fail($e->getError(),[], $e->getCode());
  22. //数据库错误
  23. if($e instanceof PDOException) return JsonService::fail($e->getMessage(),[],$e->getCode());
  24. //Db错误
  25. if($e instanceof DbException) return JsonService::fail($e->getMessage(),[],$e->getCode());
  26. //HTTP相应错误
  27. if($e instanceof HttpException) return JsonService::fail($e->getMessage(),[],$e->getCode());
  28. //其他错误异常
  29. if($e instanceof ErrorException) return JsonService::fail($e->getMessage(),[],$e->getCode());
  30. //默认错误提示
  31. $baseExcep=new BaseException();
  32. return JsonService::fail($baseExcep->msg,[],$baseExcep->code);
  33. }
  34. }