From 31236f3fc683b0474e823a6d445fd65ec63f0784 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Wed, 8 Oct 2025 17:41:40 +0530 Subject: [PATCH] fix: enhance exception handling in Elasticsearch connector and improve form validation for credentials as per coderabbitai --- .../app/connectors/elasticsearch_connector.py | 10 ++++---- .../add/elasticsearch-connector/page.tsx | 25 ++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/surfsense_backend/app/connectors/elasticsearch_connector.py b/surfsense_backend/app/connectors/elasticsearch_connector.py index 0ef536d69..4a5ceb2b1 100644 --- a/surfsense_backend/app/connectors/elasticsearch_connector.py +++ b/surfsense_backend/app/connectors/elasticsearch_connector.py @@ -4,7 +4,11 @@ from collections.abc import AsyncGenerator from typing import Any from elasticsearch import AsyncElasticsearch -from elasticsearch.exceptions import AuthenticationException, ConnectionError + +try: + from elasticsearch.exceptions import AuthenticationException +except ImportError: + AuthenticationException = Exception try: from elastic_transport import ( @@ -87,13 +91,9 @@ class ElasticsearchConnector: ) return True - # Updated exception handling for better version compatibility except (ConnectionError, TransportConnectionError, TlsError, ApiError) as e: logger.error(f"Failed to connect to Elasticsearch: {e}") return False - except AuthenticationException as e: - logger.error(f"Authentication failed for Elasticsearch: {e}") - return False except Exception as e: logger.error(f"Unexpected error connecting to Elasticsearch: {e}") return False diff --git a/surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx index 0c0df63d6..bf56a4068 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/connectors/add/elasticsearch-connector/page.tsx @@ -64,10 +64,10 @@ const elasticsearchConnectorFormSchema = z .refine( (data) => { if (data.auth_method === "basic") { - return data.username && data.password; + return Boolean(data.username?.trim() && data.password?.trim()); } if (data.auth_method === "api_key") { - return data.api_key; + return Boolean(data.api_key?.trim()); } return true; }, @@ -109,10 +109,11 @@ export default function ElasticsearchConnectorPage() { }); const stringToArray = (str: string): string[] => { - return str + const items = str .split(",") .map((item) => item.trim()) .filter((item) => item.length > 0); + return Array.from(new Set(items)); }; // Handle form submission @@ -254,6 +255,8 @@ export default function ElasticsearchConnectorPage() { Elasticsearch Endpoint URL @@ -366,7 +369,7 @@ export default function ElasticsearchConnectorPage() { Username - + @@ -380,7 +383,12 @@ export default function ElasticsearchConnectorPage() { Password - + @@ -398,7 +406,12 @@ export default function ElasticsearchConnectorPage() { API Key - + Enter your Elasticsearch API key (base64 encoded). This will be