ExceptionHandel.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/5 17:22
  5. */
  6. namespace app\index\exception;
  7. use app\common\until\Enum;
  8. use app\common\until\Until;
  9. use Error;
  10. use Exception;
  11. use think\exception\Handle;
  12. use think\exception\HttpException;
  13. use think\Response;
  14. class ExceptionHandel extends Handle {
  15. /**
  16. * Render an exception into an HTTP response.
  17. *
  18. * @access public
  19. * @param \Exception $e
  20. * @return Response
  21. */
  22. public function render(Exception $e)
  23. {
  24. // 添加自定义异常处理机制
  25. if ($e instanceof ApiException) {
  26. Until::output([], $e->getMessage(),Enum::THROW_ERR_CODE);
  27. }
  28. if ($e instanceof TokenException) {
  29. Until::output([], $e->getMessage(),Enum::REFRSEH_TOKEN);
  30. }
  31. if ($e instanceof Exception || $e instanceof Error) {
  32. Until::output([], $e->getMessage().' file:' . str_replace('/mnt/www','ROOT',$e->getFile()) . ' line:' . $e->getLine() ,Enum::THROW_ERR_CODE);
  33. }
  34. // 其他错误交给系统处理
  35. return parent::render($e);
  36. }
  37. }