67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Http\Request;
|
|
|
|
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('/admin/login');
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
|
$exceptions->shouldRenderJsonWhen(
|
|
fn (Request $request) => $request->is('api/*')
|
|
);
|
|
|
|
$exceptions->render(function (ModelNotFoundException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data tidak ditemukan.',
|
|
], 404);
|
|
});
|
|
|
|
$exceptions->render(function (
|
|
NotFoundHttpException $e,
|
|
Request $request
|
|
) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data tidak ditemukan.',
|
|
], 404);
|
|
});
|
|
|
|
$exceptions->render(function (AuthenticationException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Unauthenticated.',
|
|
], 401);
|
|
});
|
|
|
|
$exceptions->render(function (ValidationException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Validasi gagal.',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
});
|
|
|
|
$exceptions->render(function (HttpException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage() ?: 'Terjadi kesalahan.',
|
|
], $e->getStatusCode());
|
|
});
|
|
})->create();
|