leptos-rs/leptos · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

BrowserMockRes::try_from_string() always panics with unreachable!(). Per the source docs, this type 'always panics if its methods are called' and exists solely to stub out the server response type when compiling for the client. Hitting this panic means code tried to construct a server response in a browser context where no real response can be built.

Source

Thrown at server_fn/src/response/mod.rs:91

    /// The `Location` header or (if none is set), the URL of the response.
    fn location(&self) -> String;

    /// Whether the response has the [`REDIRECT_HEADER`](crate::redirect::REDIRECT_HEADER) set.
    fn has_redirect(&self) -> bool;
}

/// A mocked response type that can be used in place of the actual server response,
/// when compiling for the browser.
///
/// ## Panics
/// This always panics if its methods are called. It is used solely to stub out the
/// server response type when compiling for the client.
pub struct BrowserMockRes;

impl<E> TryRes<E> for BrowserMockRes {
    fn try_from_string(_content_type: &str, _data: String) -> Result<Self, E> {
        unreachable!()
    }

    fn try_from_bytes(_content_type: &str, _data: Bytes) -> Result<Self, E> {
        unreachable!()
    }

    fn try_from_stream(
        _content_type: &str,
        _data: impl Stream<Item = Result<Bytes, Bytes>>,
    ) -> Result<Self, E> {
        unreachable!()
    }
}

impl Res for BrowserMockRes {
    fn error_response(_path: &str, _err: Bytes) -> Self {
        unreachable!()
    }

View on GitHub (pinned to 32d20f6c9d)

Solutions

  1. Ensure response construction happens only in server fn code running on the server.
  2. On the client, let the framework handle the returned value instead of building an HTTP response.
  3. Gate response-building helpers behind #[cfg(feature = "server")].

Example fix

// before
let res = BrowserMockRes::try_from_string("text/plain", data)?;
// after: build responses only in server fns
#[server]
fn handler() -> Result<String, ServerFnError> { Ok(data) }
Defensive patterns

Strategy: validation

Validate before calling

if !cfg!(feature = "server") { return None; } // never construct server responses on client

Type guard

fn can_build_res() -> bool { cfg!(feature = "server") }

Try / catch

#[cfg(feature = "server")]
let res = TryRes::try_from_string(ct, data)?;

Prevention

When it happens

Trigger: Calling TryRes::try_from_string() on BrowserMockRes — constructing a server-fn HTTP response from a string while running client-side.

Common situations: Server response-building code executed in wasm builds; custom server fn return handling invoked from the client; build/feature misconfiguration in leptos apps.

Related errors


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