feat: do not exit app and save errors
This commit is contained in:
81
main.py
81
main.py
@@ -448,8 +448,11 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
||||
)
|
||||
|
||||
# Validate credentials and configuration by testing actual resources
|
||||
# These validations are non-blocking - errors are logged but won't stop startup
|
||||
log_structured_entry("Starting validation of credentials and resources", "INFO")
|
||||
|
||||
validation_errors = []
|
||||
|
||||
# 1. Validate GenAI embedding access
|
||||
log_structured_entry("Validating GenAI embedding access", "INFO")
|
||||
try:
|
||||
@@ -468,15 +471,15 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
||||
)
|
||||
else:
|
||||
msg = "Embedding validation returned empty response"
|
||||
log_structured_entry(msg, "ERROR")
|
||||
raise RuntimeError(msg)
|
||||
log_structured_entry(msg, "WARNING")
|
||||
validation_errors.append(msg)
|
||||
except Exception as e:
|
||||
log_structured_entry(
|
||||
"Failed to validate GenAI embedding access",
|
||||
"ERROR",
|
||||
"Failed to validate GenAI embedding access - service may not work correctly",
|
||||
"WARNING",
|
||||
{"error": str(e), "error_type": type(e).__name__}
|
||||
)
|
||||
raise
|
||||
validation_errors.append(f"GenAI: {str(e)}")
|
||||
|
||||
# 2. Validate GCS bucket access
|
||||
log_structured_entry(
|
||||
@@ -500,43 +503,41 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
||||
if response.status == 403:
|
||||
msg = f"Access denied to bucket '{cfg.bucket}'. Check permissions."
|
||||
log_structured_entry(
|
||||
"GCS bucket validation failed - access denied",
|
||||
"ERROR",
|
||||
"GCS bucket validation failed - access denied - service may not work correctly",
|
||||
"WARNING",
|
||||
{"bucket": cfg.bucket, "status": response.status}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
validation_errors.append(msg)
|
||||
elif response.status == 404:
|
||||
msg = f"Bucket '{cfg.bucket}' not found. Check bucket name and project."
|
||||
log_structured_entry(
|
||||
"GCS bucket validation failed - not found",
|
||||
"ERROR",
|
||||
"GCS bucket validation failed - not found - service may not work correctly",
|
||||
"WARNING",
|
||||
{"bucket": cfg.bucket, "status": response.status}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
validation_errors.append(msg)
|
||||
elif not response.ok:
|
||||
body = await response.text()
|
||||
msg = f"Failed to access bucket '{cfg.bucket}': {response.status} - {body}"
|
||||
msg = f"Failed to access bucket '{cfg.bucket}': {response.status}"
|
||||
log_structured_entry(
|
||||
"GCS bucket validation failed",
|
||||
"ERROR",
|
||||
"GCS bucket validation failed - service may not work correctly",
|
||||
"WARNING",
|
||||
{"bucket": cfg.bucket, "status": response.status, "response": body}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
|
||||
validation_errors.append(msg)
|
||||
else:
|
||||
log_structured_entry(
|
||||
"GCS bucket validation successful",
|
||||
"INFO",
|
||||
{"bucket": cfg.bucket}
|
||||
)
|
||||
except RuntimeError:
|
||||
raise
|
||||
except Exception as e:
|
||||
log_structured_entry(
|
||||
"Failed to validate GCS bucket access",
|
||||
"ERROR",
|
||||
"Failed to validate GCS bucket access - service may not work correctly",
|
||||
"WARNING",
|
||||
{"error": str(e), "error_type": type(e).__name__, "bucket": cfg.bucket}
|
||||
)
|
||||
raise
|
||||
validation_errors.append(f"GCS: {str(e)}")
|
||||
|
||||
# 3. Validate vector search endpoint access
|
||||
log_structured_entry(
|
||||
@@ -556,44 +557,50 @@ async def lifespan(_server: FastMCP) -> AsyncIterator[AppContext]:
|
||||
if response.status == 403:
|
||||
msg = f"Access denied to endpoint '{cfg.endpoint_name}'. Check permissions."
|
||||
log_structured_entry(
|
||||
"Vector search endpoint validation failed - access denied",
|
||||
"ERROR",
|
||||
"Vector search endpoint validation failed - access denied - service may not work correctly",
|
||||
"WARNING",
|
||||
{"endpoint": cfg.endpoint_name, "status": response.status}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
validation_errors.append(msg)
|
||||
elif response.status == 404:
|
||||
msg = f"Endpoint '{cfg.endpoint_name}' not found. Check endpoint name and project."
|
||||
log_structured_entry(
|
||||
"Vector search endpoint validation failed - not found",
|
||||
"ERROR",
|
||||
"Vector search endpoint validation failed - not found - service may not work correctly",
|
||||
"WARNING",
|
||||
{"endpoint": cfg.endpoint_name, "status": response.status}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
validation_errors.append(msg)
|
||||
elif not response.ok:
|
||||
body = await response.text()
|
||||
msg = f"Failed to access endpoint '{cfg.endpoint_name}': {response.status} - {body}"
|
||||
msg = f"Failed to access endpoint '{cfg.endpoint_name}': {response.status}"
|
||||
log_structured_entry(
|
||||
"Vector search endpoint validation failed",
|
||||
"ERROR",
|
||||
"Vector search endpoint validation failed - service may not work correctly",
|
||||
"WARNING",
|
||||
{"endpoint": cfg.endpoint_name, "status": response.status, "response": body}
|
||||
)
|
||||
raise RuntimeError(msg)
|
||||
|
||||
validation_errors.append(msg)
|
||||
else:
|
||||
log_structured_entry(
|
||||
"Vector search endpoint validation successful",
|
||||
"INFO",
|
||||
{"endpoint": cfg.endpoint_name}
|
||||
)
|
||||
except RuntimeError:
|
||||
raise
|
||||
except Exception as e:
|
||||
log_structured_entry(
|
||||
"Failed to validate vector search endpoint access",
|
||||
"ERROR",
|
||||
"Failed to validate vector search endpoint access - service may not work correctly",
|
||||
"WARNING",
|
||||
{"error": str(e), "error_type": type(e).__name__, "endpoint": cfg.endpoint_name}
|
||||
)
|
||||
raise
|
||||
validation_errors.append(f"Vector Search: {str(e)}")
|
||||
|
||||
# Summary of validations
|
||||
if validation_errors:
|
||||
log_structured_entry(
|
||||
"MCP server started with validation errors - service may not work correctly",
|
||||
"WARNING",
|
||||
{"validation_errors": validation_errors, "error_count": len(validation_errors)}
|
||||
)
|
||||
else:
|
||||
log_structured_entry("All validations passed - MCP server initialization complete", "INFO")
|
||||
|
||||
yield AppContext(
|
||||
|
||||
Reference in New Issue
Block a user