支付宝记账本
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.

64 lines
2.4 KiB

5 months ago
  1. <?php
  2. use App\Libs\AppException;
  3. use Illuminate\Auth\AuthenticationException;
  4. use Illuminate\Foundation\Application;
  5. use Illuminate\Foundation\Configuration\Exceptions;
  6. use Illuminate\Foundation\Configuration\Middleware;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Validation\ValidationException;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  12. return Application::configure(basePath: dirname(__DIR__))
  13. ->withRouting(
  14. web: __DIR__.'/../routes/web.php',
  15. api: __DIR__.'/../routes/api.php',
  16. commands: __DIR__.'/../routes/console.php',
  17. health: '/up',
  18. )
  19. ->withMiddleware(function (Middleware $middleware): void {
  20. $middleware->redirectGuestsTo(function (Request $request) {
  21. if ($request->is('api/*')) {
  22. throw new HttpException(Response::HTTP_UNAUTHORIZED);
  23. }
  24. });
  25. $middleware->throttleApi('api', true);
  26. })
  27. ->withExceptions(function (Exceptions $exceptions): void {
  28. $exceptions->dontReport(AppException::class);
  29. $exceptions->respond(function (Response $response, Throwable $e, Request $request) {
  30. if (!$request->is('api/*')) {
  31. return $response;
  32. }
  33. switch ($e) {
  34. case $e instanceof AppException:
  35. $msg = $e->getMessage();
  36. break;
  37. case $e instanceof ValidationException:
  38. $msg = $e->validator->errors()->first();
  39. break;
  40. case $e instanceof AuthenticationException:
  41. $code = Response::HTTP_UNAUTHORIZED;
  42. $msg = __("http-statuses.$code");
  43. break;
  44. case $e instanceof HttpExceptionInterface:
  45. $code = $e->getStatusCode();
  46. $msg = __("http-statuses.$code");
  47. break;
  48. default:
  49. $code = Response::HTTP_INTERNAL_SERVER_ERROR;
  50. if (config('app.debug')) {
  51. $msg = $e->getMessage() ?: get_class($e);
  52. } else {
  53. $msg = __("http-statuses.$code");
  54. }
  55. }
  56. return error($msg);
  57. });
  58. })->create();