AdminException.php 877 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/10
  6. */
  7. namespace app\admin\controller;
  8. use think\exception\Handle;
  9. use think\exception\ValidateException;
  10. use think\Response;
  11. use Throwable;
  12. /**
  13. * 后台异常处理
  14. *
  15. * Class AdminException
  16. * @package app\admin\controller
  17. */
  18. class AdminException extends Handle
  19. {
  20. public function render($request, Throwable $e): Response
  21. {
  22. // 参数验证错误
  23. if ($e instanceof ValidateException) {
  24. return app('json')->make(422, $e->getError());
  25. }
  26. if ($e instanceof \Exception && request()->isAjax()) {
  27. return app('json')->fail($e->getMessage(), ['code' => $e->getCode(), 'line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  28. }
  29. return parent::render($request, $e);
  30. }
  31. }