2
0

🔊 Add logs to debug invalid env

This commit is contained in:
Baptiste Arnaud
2023-08-30 18:47:57 +02:00
parent 124cb8f359
commit 96ddfadaa7
3 changed files with 23 additions and 4 deletions

21
packages/env/env.ts vendored
View File

@ -1,7 +1,11 @@
import { createEnv } from '@t3-oss/env-nextjs'
import { z } from 'zod'
import { ZodError, z } from 'zod'
import { getRuntimeVariable } from './getRuntimeVariable'
declare const window: {
__ENV?: any
}
const boolean = z.enum(['true', 'false']).transform((value) => value === 'true')
const baseEnv = {
@ -313,6 +317,21 @@ export const env = createEnv({
...sentryEnv.runtimeEnv,
...posthogEnv.runtimeEnv,
},
onValidationError: (error: ZodError) => {
console.log(
'[DEBUG]',
"typeof window !== 'undefined'",
typeof window !== 'undefined'
)
if (typeof window !== 'undefined') {
console.log('[DEBUG]', 'window.__ENV', window.__ENV)
}
console.error(
'❌ Invalid environment variables:',
error.flatten().fieldErrors
)
throw new Error('Invalid environment variables')
},
onInvalidAccess: (variable: string) => {
throw new Error(
`❌ Attempted to access a server-side environment variable on the client: ${variable}`