api
This commit is contained in:
+46
-1
@@ -3,11 +3,17 @@
|
||||
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',
|
||||
)
|
||||
@@ -16,6 +22,45 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions): void {
|
||||
$exceptions->shouldRenderJsonWhen(
|
||||
fn (Request $request) => $request->is('api/*'),
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user