getStatusCode($e); $message = $this->getMessage($e); // Log the exception with essential context for debugging $this->logger->error('Exception handled: ' . get_class($e), [ 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'status_code' => $statusCode, ]); return new DataResponse(['message' => $message], $statusCode); } private function getStatusCode(Exception $e): int { return match (true) { $e instanceof NotFoundException => Http::STATUS_NOT_FOUND, $e instanceof NotPermittedException => Http::STATUS_FORBIDDEN, $e instanceof UnauthorizedException => Http::STATUS_UNAUTHORIZED, $e instanceof InvalidUserException => Http::STATUS_BAD_REQUEST, default => (int)($e->getCode() ?: Http::STATUS_INTERNAL_SERVER_ERROR), }; } private function getMessage(Exception $e): string { return match (true) { $e instanceof NotFoundException => 'File not found', $e instanceof NotPermittedException => 'Permission denied', $e instanceof UnauthorizedException => 'Unauthorized', $e instanceof InvalidUserException => 'Invalid user', default => $e->getMessage() ?: 'An error occurred', }; } }