Spaces:
Running
Running
| use Illuminate\Foundation\Application; | |
| use Illuminate\Foundation\Configuration\Exceptions; | |
| use Illuminate\Foundation\Configuration\Middleware; | |
| use Symfony\Component\HttpKernel\Exception\HttpException; | |
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
| use Symfony\Component\Routing\Exception\RouteNotFoundException; | |
| use Illuminate\Auth\AuthenticationException; | |
| use Illuminate\Validation\ValidationException; | |
| 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->trustProxies(at: '*'); | |
| $middleware->alias([ | |
| 'job.owner' => App\Http\Middleware\CheckJobOwnership::class, | |
| 'subscribed' => \App\Http\Middleware\EnsureActiveSubscription::class, | |
| 'pro' => \App\Http\Middleware\EnsureProPlan::class, | |
| 'max' => \App\Http\Middleware\EnsureMaxPlan::class, | |
| ]); | |
| }) | |
| ->withExceptions(function (Exceptions $exceptions): void { | |
| // NotFoundHttpException | |
| $exceptions->renderable(function (NotFoundHttpException $e, $request) { | |
| if ($request->is('api/*')) { | |
| return response()->json([ | |
| 'success' => false, | |
| 'message' => $e->getMessage() ?: 'Resource not found' | |
| ], 404); | |
| } | |
| }); | |
| // AuthenticationException | |
| $exceptions->renderable(function (AuthenticationException $e, $request) { | |
| if ($request->is('api/*')) { | |
| return response()->json([ | |
| 'success' => false, | |
| 'message' => $e->getMessage() ?: 'Unauthenticated' | |
| ], 401); | |
| } | |
| }); | |
| // RouteNotFoundException (auth middleware redirecting to non-existent login route) | |
| $exceptions->renderable(function (RouteNotFoundException $e, $request) { | |
| if ($request->is('api/*')) { | |
| return response()->json([ | |
| 'success' => false, | |
| 'message' => 'Unauthenticated' | |
| ], 401); | |
| } | |
| }); | |
| // ValidationException | |
| $exceptions->renderable(function (ValidationException $e, $request) { | |
| if ($request->is('api/*')) { | |
| return response()->json([ | |
| 'success' => false, | |
| 'message' => $e->getMessage() ?: 'Validation error', | |
| 'errors' => $e->errors() | |
| ], 422); | |
| } | |
| }); | |
| // Catch-all for any other exceptions | |
| $exceptions->renderable(function (\Throwable $e, $request) { | |
| if ($request->is('api/*')) { | |
| $status = method_exists($e, 'getStatusCode') && $e instanceof HttpException ? $e->getStatusCode() : 500; | |
| return response()->json([ | |
| 'success' => false, | |
| 'message' => $e->getMessage() ?: 'Server Error' | |
| ], $status); | |
| } | |
| }); | |
| })->create(); | |