fix: enhance exception handling in Elasticsearch connector and improve form validation for credentials as per coderabbitai

This commit is contained in:
Anish Sarkar 2025-10-08 17:41:40 +05:30
parent a7ad21d7fa
commit 31236f3fc6
2 changed files with 24 additions and 11 deletions

View file

@ -4,7 +4,11 @@ from collections.abc import AsyncGenerator
from typing import Any from typing import Any
from elasticsearch import AsyncElasticsearch from elasticsearch import AsyncElasticsearch
from elasticsearch.exceptions import AuthenticationException, ConnectionError
try:
from elasticsearch.exceptions import AuthenticationException
except ImportError:
AuthenticationException = Exception
try: try:
from elastic_transport import ( from elastic_transport import (
@ -87,13 +91,9 @@ class ElasticsearchConnector:
) )
return True return True
# Updated exception handling for better version compatibility
except (ConnectionError, TransportConnectionError, TlsError, ApiError) as e: except (ConnectionError, TransportConnectionError, TlsError, ApiError) as e:
logger.error(f"Failed to connect to Elasticsearch: {e}") logger.error(f"Failed to connect to Elasticsearch: {e}")
return False return False
except AuthenticationException as e:
logger.error(f"Authentication failed for Elasticsearch: {e}")
return False
except Exception as e: except Exception as e:
logger.error(f"Unexpected error connecting to Elasticsearch: {e}") logger.error(f"Unexpected error connecting to Elasticsearch: {e}")
return False return False

View file

@ -64,10 +64,10 @@ const elasticsearchConnectorFormSchema = z
.refine( .refine(
(data) => { (data) => {
if (data.auth_method === "basic") { if (data.auth_method === "basic") {
return data.username && data.password; return Boolean(data.username?.trim() && data.password?.trim());
} }
if (data.auth_method === "api_key") { if (data.auth_method === "api_key") {
return data.api_key; return Boolean(data.api_key?.trim());
} }
return true; return true;
}, },
@ -109,10 +109,11 @@ export default function ElasticsearchConnectorPage() {
}); });
const stringToArray = (str: string): string[] => { const stringToArray = (str: string): string[] => {
return str const items = str
.split(",") .split(",")
.map((item) => item.trim()) .map((item) => item.trim())
.filter((item) => item.length > 0); .filter((item) => item.length > 0);
return Array.from(new Set(items));
}; };
// Handle form submission // Handle form submission
@ -254,6 +255,8 @@ export default function ElasticsearchConnectorPage() {
<FormLabel>Elasticsearch Endpoint URL</FormLabel> <FormLabel>Elasticsearch Endpoint URL</FormLabel>
<FormControl> <FormControl>
<Input <Input
type="url"
autoComplete="off"
placeholder="https://your-cluster.es.region.aws.com:443" placeholder="https://your-cluster.es.region.aws.com:443"
{...field} {...field}
/> />
@ -366,7 +369,7 @@ export default function ElasticsearchConnectorPage() {
<FormItem> <FormItem>
<FormLabel>Username</FormLabel> <FormLabel>Username</FormLabel>
<FormControl> <FormControl>
<Input placeholder="elastic" {...field} /> <Input placeholder="elastic" autoComplete="username" {...field} />
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
@ -380,7 +383,12 @@ export default function ElasticsearchConnectorPage() {
<FormItem> <FormItem>
<FormLabel>Password</FormLabel> <FormLabel>Password</FormLabel>
<FormControl> <FormControl>
<Input type="password" placeholder="Password" {...field} /> <Input
type="password"
placeholder="Password"
autoComplete="current-password"
{...field}
/>
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
@ -398,7 +406,12 @@ export default function ElasticsearchConnectorPage() {
<FormItem> <FormItem>
<FormLabel>API Key</FormLabel> <FormLabel>API Key</FormLabel>
<FormControl> <FormControl>
<Input type="password" placeholder="Your API Key Here" {...field} /> <Input
type="password"
placeholder="Your API Key Here"
autoComplete="off"
{...field}
/>
</FormControl> </FormControl>
<FormDescription> <FormDescription>
Enter your Elasticsearch API key (base64 encoded). This will be Enter your Elasticsearch API key (base64 encoded). This will be