ExceptionHandle.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Core\Exception;
  4. use think\cors\HandleCors;
  5. use think\db\exception\ModelNotFoundException;
  6. use think\exception\Handle;
  7. use think\exception\ValidateException;
  8. use think\Request;
  9. use think\Response;
  10. use Throwable;
  11. use function SixShop\Core\error_response;
  12. class ExceptionHandle extends Handle
  13. {
  14. public function render(Request $request, Throwable $e): Response
  15. {
  16. if ($e instanceof ModelNotFoundException) {
  17. $e = new NotFoundException($e);
  18. }
  19. if ($e instanceof ValidateException) {
  20. $e = new LogicException(error_response(msg: $e->getMessage(), status: 'invalid_argument', code: 400, httpCode: 200));
  21. }
  22. $response = parent::render($request, $e);
  23. return $this->app->make(HandleCors::class)->handle($request, function () use ($request, $response) {
  24. return $response;
  25. });
  26. }
  27. protected function getDebugMsg(Throwable $exception): array
  28. {
  29. $debugInfo = parent::getDebugMsg($exception);
  30. $debugInfo['data'] = $debugInfo['datas'];
  31. $debugInfo['msg'] = $debugInfo['message'];
  32. unset($debugInfo['datas'], $debugInfo['message']);
  33. return $debugInfo;
  34. }
  35. protected function getDeployMsg(Throwable $exception): array
  36. {
  37. $deployInfo = parent::getDeployMsg($exception);
  38. $deployInfo['msg'] = $deployInfo['message'];
  39. unset($deployInfo['message']);
  40. return $deployInfo;
  41. }
  42. }