expectsJson()) { return response()->json([ 'status' => false, 'error' => 401, 'message' => trans('app.errors.401') ], 401); } return redirect()->guest('login'); } /** * @param Throwable $exception * @throws \Exception */ public function report(Throwable $exception) { // if (app()->bound('sentry') && $this->shouldReport($exception)) { // $user = request()->user(); // // if (!empty($user)) { // //$token = Token::where('token', $token)->first(); // app('sentry')->configureScope(function (Scope $scope) use ($user): void { // $scope->setUser([ // 'id' => $user->id, // 'phone' => $user->phone, // ]); // }); // } // // app('sentry')->captureException($exception); // } if (app()->bound('sentry') && $this->shouldReport($exception)) { app('sentry')->captureException($exception); } parent::report($exception); } /** * @param \Illuminate\Http\Request $request * @param Throwable $exception * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response * @throws Throwable */ public function render($request, Throwable $exception) { // Check if the request expects JSON if ($request->expectsJson()) { // Handle 404 Not Found exception if ($exception instanceof NotFoundHttpException) { return response()->json([ 'error' => 'Resource not found' ], 404); } // Handle validation exceptions if ($exception instanceof ValidationException) { return response()->json([ 'error' => 'Validation error', 'details' => $exception->errors(), ], 422); } // Handle generic exceptions return response()->json([ 'error' => $exception->getMessage(), ], $this->isHttpException($exception) ? $exception->getStatusCode() : 500); } // For non-API requests, use the default render method return parent::render($request, $exception); } }