withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // Apply ForceJsonResponse only to API routes $middleware->api(ForceJsonResponse::class); }) ->withExceptions(function (Exceptions $exceptions) { $exceptions->shouldRenderJsonWhen(function (Request $request, Throwable $e) { if ($request->is('api/*')) { return true; } return $request->expectsJson(); }); /** @var OutputService $outputService */ $outputService = resolve(OutputService::class); $exceptions->render(function (ValidationException $e, Request $request) use ($outputService) { if ($request->is('api/*') || $request->expectsJson()) { return response()->json( $outputService->response(false, null, [$e->getMessage()]), 404 ); } }); $exceptions->render(function (NotFoundHttpException $e, Request $request) use ($outputService) { if ($request->is('api/*') || $request->expectsJson()) { return response()->json( $outputService->response(false, null, ['MODEL_NOT_FOUND']), 404 ); } }); $exceptions->render(function (AccessDeniedHttpException $e, Request $request) use ($outputService) { if ($request->is('api/*') || $request->expectsJson()) { return response()->json( $outputService->response(false, null, [$e->getMessage()]), 403 ); } }); }) ->withCommands([ GenerateScheduleCommand::class, ]) ->create();