fixed (i think)

This commit is contained in:
Michael Freno
2025-12-19 15:52:44 -05:00
parent 004ab5407f
commit bc90adf839
4 changed files with 101 additions and 92 deletions

51
src/env/server.ts vendored
View File

@@ -177,29 +177,42 @@ export const validateClientEnv = (
}
};
// Environment validation for server startup with better error reporting
export const env = (() => {
try {
// Validate server environment variables using process.env
const validatedServerEnv = validateServerEnv(process.env);
// Lazy environment validation - only validates when accessed
let _cachedEnv: ServerEnv | null = null;
let _validationAttempted = false;
console.log("✅ Environment validation successful");
return validatedServerEnv;
} catch (error) {
if (error instanceof EnvironmentError) {
console.error("❌ Environment validation failed:", error.message);
if (error.errors) {
console.error(
"Detailed errors:",
JSON.stringify(error.errors, null, 2)
);
export const env = new Proxy({} as ServerEnv, {
get(_target, prop: string) {
// Only validate once
if (!_validationAttempted) {
_validationAttempted = true;
try {
// Validate server environment variables using process.env
_cachedEnv = validateServerEnv(process.env);
console.log("✅ Environment validation successful");
} catch (error) {
if (error instanceof EnvironmentError) {
console.error("❌ Environment validation failed:", error.message);
if (error.errors) {
console.error(
"Detailed errors:",
JSON.stringify(error.errors, null, 2)
);
}
throw new Error(`Environment validation failed: ${error.message}`);
}
console.error("❌ Unexpected environment validation error:", error);
throw new Error("Unexpected environment validation error occurred");
}
throw new Error(`Environment validation failed: ${error.message}`);
}
console.error("❌ Unexpected environment validation error:", error);
throw new Error("Unexpected environment validation error occurred");
if (!_cachedEnv) {
throw new Error("Environment validation has not been performed yet");
}
return _cachedEnv[prop as keyof ServerEnv];
}
})();
});
// For client-side validation (useful in components)
export const getClientEnvValidation = () => {