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
64 lines
2.4 KiB
<?php
|
|
|
|
use App\Libs\AppException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
)
|
|
->withMiddleware(function (Middleware $middleware): void {
|
|
$middleware->redirectGuestsTo(function (Request $request) {
|
|
if ($request->is('api/*')) {
|
|
throw new HttpException(Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
});
|
|
|
|
$middleware->throttleApi('api', true);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
|
$exceptions->dontReport(AppException::class);
|
|
|
|
$exceptions->respond(function (Response $response, Throwable $e, Request $request) {
|
|
if (!$request->is('api/*')) {
|
|
return $response;
|
|
}
|
|
|
|
switch ($e) {
|
|
case $e instanceof AppException:
|
|
$msg = $e->getMessage();
|
|
break;
|
|
case $e instanceof ValidationException:
|
|
$msg = $e->validator->errors()->first();
|
|
break;
|
|
case $e instanceof AuthenticationException:
|
|
$code = Response::HTTP_UNAUTHORIZED;
|
|
$msg = __("http-statuses.$code");
|
|
break;
|
|
case $e instanceof HttpExceptionInterface:
|
|
$code = $e->getStatusCode();
|
|
$msg = __("http-statuses.$code");
|
|
break;
|
|
default:
|
|
$code = Response::HTTP_INTERNAL_SERVER_ERROR;
|
|
if (config('app.debug')) {
|
|
$msg = $e->getMessage() ?: get_class($e);
|
|
} else {
|
|
$msg = __("http-statuses.$code");
|
|
}
|
|
}
|
|
|
|
return error($msg);
|
|
});
|
|
})->create();
|