leptos-rs/leptos · error · LeptosConfigError

{}

Error message

{}

What it means

`impl From<&str> for Env` parses a string into the Env enum via env_from_str and panics with the parse error when the string is not a recognized environment name. This is used when converting user-provided config strings, so an invalid value aborts instead of falling back.

Source

Thrown at leptos_config/src/lib.rs:332

        ENV_PROD_KEY_SHORT | ENV_PROD_KEY_LONG => Ok(Env::PROD),
        _ => Err(LeptosConfigError::EnvVarError(format!(
            "{input} is not a supported environment. Use either \
             `{ENV_DEV_KEY_SHORT}`, `{ENV_DEV_KEY_LONG}`, \
             `{ENV_PROD_KEY_SHORT}`, or `{ENV_PROD_KEY_LONG}`.",
        ))),
    }
}

impl FromStr for Env {
    type Err = ();
    fn from_str(input: &str) -> Result<Self, Self::Err> {
        env_from_str(input).or_else(|_| Ok(Self::default()))
    }
}

impl From<&str> for Env {
    fn from(str: &str) -> Self {
        env_from_str(str).unwrap_or_else(|err| panic!("{}", err))
    }
}

impl From<&Result<String, VarError>> for Env {
    fn from(input: &Result<String, VarError>) -> Self {
        match input {
            Ok(str) => {
                env_from_str(str).unwrap_or_else(|err| panic!("{}", err))
            }
            Err(_) => Self::default(),
        }
    }
}

impl TryFrom<String> for Env {
    type Error = LeptosConfigError;

    fn try_from(s: String) -> Result<Self, Self::Error> {

View on GitHub (pinned to 32d20f6c9d)

Solutions

  1. Set LEPTOS_ENV (or the string being converted) to a valid value: dev/development, prod/production, or test.
  2. If a default should be used instead of panicking, call env_from_str and handle the Err yourself.
  3. Use the &Result<String, VarError> conversion (Err falls back to default) instead of From<&str> for raw env-var reads.

Example fix

// before
let env: Env = "production".to_string().as_str().into(); // panics on typo'd input
// after
let env = env_from_str("production").unwrap_or(Env::default());
Defensive patterns

Strategy: validation

Validate before calling

fn parse_env_safe(s: &str) -> Option<Env> { env_from_str(s).ok() }

Type guard

fn is_valid_env(s: &str) -> bool {
    matches!(s.to_lowercase().as_str(), "dev" | "development" | "prod" | "production" | "test")
}

Try / catch

// Env::from panics, so validate first:
let raw = std::env::var("LEPTOS_ENV").unwrap_or_default();
let env = if is_valid_env(&raw) { raw.as_str().into() } else { Env::default() };

Prevention

When it happens

Trigger: Calling `.into()` / `Env::from(some_str)` with a string that is not a recognized environment (dev/development, prod/production, test variants accepted by env_from_str).

Common situations: Setting LEPTOS_ENV to a typo like "prodaction" in .env or the environment; loading config where env is read from a string and converted with From<&str>.

Related errors


AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01). Data as JSON: /api/errors/d6fda01a2c2f9ae4. Report an issue: GitHub.