{"record":{"id":"fb0de3d8fced11a6","repo":"clockworklabs/SpacetimeDB","slug":"invalid-environment-read-response","errorCode":null,"errorMessage":"Invalid environment read response","messagePattern":"Invalid environment read response","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/subcommands/env.rs","lineNumber":122,"sourceCode":"    );\n    let mut body = Vec::new();\n    let limit = MAX_ENV_VARS * (MAX_ENV_KEY_BYTES + MAX_ENV_VALUE_BYTES) * 6 + 64 * 1024;\n    let mut stream = response.bytes_stream();\n    while let Some(chunk) = stream.next().await {\n        let chunk = chunk?;\n        ensure!(\n            body.len().saturating_add(chunk.len()) <= limit,\n            \"Environment read response exceeds limit\"\n        );\n        body.extend_from_slice(&chunk);\n    }\n    render(&body, &query)\n}\n\nfn render(body: &[u8], query: &Query) -> anyhow::Result<String> {\n    // Validate the requested projection before rendering any response values.\n    let results: Vec<spacetimedb_client_api_messages::http::SqlStmtResult<Vec<String>>> =\n        serde_json::from_slice(body).map_err(|_| anyhow::anyhow!(\"Invalid environment read response\"))?;\n    ensure!(results.len() == 1, \"Invalid environment read result count\");\n    let result = &results[0];\n    let expected: &[&str] = match query {\n        Query::List => &[\"key\", \"value\"],\n        Query::Get(_) => &[\"value\"],\n    };\n    ensure!(\n        result.schema.elements.len() == expected.len()\n            && result.schema.elements.iter().zip(expected).all(|(column, name)| {\n                column.name.as_deref() == Some(*name) && column.algebraic_type == spacetimedb_lib::AlgebraicType::String\n            }),\n        \"Invalid environment read projection\"\n    );\n    ensure!(result.rows.len() <= MAX_ENV_VARS, \"Environment key count exceeds limit\");\n    for row in &result.rows {\n        ensure!(row.len() == expected.len(), \"Invalid environment read row\");\n        if matches!(query, Query::List) {\n            validate_key(&row[0]).context(\"Invalid environment key in response\")?;","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/cli/src/subcommands/env.rs#L104-L140","documentation":"Thrown by `render` when the accumulated response body of an environment read cannot be parsed as JSON into `Vec<spacetimedb_client_api_messages::http::SqlStmtResult<Vec<String>>>`. The CLI expects the HTTP SQL endpoint's standard statement-result shape; anything else is rejected before rendering.","triggerScenarios":"`serde_json::from_slice(body)` fails — the server returned non-JSON (HTML error page, empty body, truncated stream) or JSON in an unexpected shape (object instead of array, different field types).","commonSituations":"CLI version mismatched with server version so the SQL response schema differs; a proxy returning an HTML error page; hitting the wrong URL path.","solutions":["Check that CLI and server versions match (`spacetime version`); upgrade or downgrade to align them.","Inspect the raw response (e.g. with `curl`) to see what the server actually returned.","Confirm you are calling the /v1/database/sql endpoint of a SpacetimeDB server.","Retry in case the response was truncated by a network fault."],"exampleFix":"// server returned HTML due to wrong URL\n// before\n--server https://example.com        # serves HTML\n// after\n--server https://spacetimedb.example.com   # actual SpacetimeDB node","handlingStrategy":"validation","validationCode":"// before parsing, sanity-check the body looks like JSON array output\nif !body.trim_start().starts_with('[') {\n    return Err(\"server did not return SQL statement results\");\n}","typeGuard":"fn looks_like_stmt_results(body: &[u8]) -> bool {\n    serde_json::from_slice::<serde_json::Value>(body)\n        .map(|v| v.is_array())\n        .unwrap_or(false)\n}","tryCatchPattern":null,"preventionTips":["Keep CLI and server versions aligned.","Do not place HTML-generating proxies in front of the SQL endpoint.","Log raw response bodies on parse failure for diagnosis."],"tags":["cli","json","spacetimedb","response-parsing"],"backgroundTag":"json-parse-error","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-09-20T12:15:59.611Z","contentChangedAt":"2026-09-20T12:15:59.611Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}