{"record":{"id":"3f388927c0ceb5e5","repo":"getzola/zola","slug":"could-not-build-error-response","errorCode":null,"errorMessage":"Could not build error response","messagePattern":"Could not build error response","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/cmd/serve.rs","lineNumber":340,"sourceCode":"            r#\"<div style=\"all:revert;position:fixed;display:flex;align-items:center;justify-content:center;background-color:rgb(0,0,0,0.5);top:0;right:0;bottom:0;left:0;\"><div style=\"background-color:white;padding:0.5rem;border-radius:0.375rem;filter:drop-shadow(0,25px,25px,rgb(0,0,0/0.15));overflow-x:auto;\"><p style=\"font-weight:700;color:black;font-size:1.25rem;margin:0;margin-bottom:0.5rem;\">Zola Build Error:</p><pre style=\"padding:0.5rem;margin:0;border-radius:0.375rem;background-color:#363636;color:#CE4A2F;font-weight:700;\">{error_str}</pre></div></div>\"#\n        );\n\n        if is_html {\n            // Inject error dialog into existing HTML response\n            let mut new_bytes = bytes;\n            new_bytes.extend(html_error.as_bytes());\n            return Response::from_parts(parts, Body::from(new_bytes));\n        } else if is_not_found {\n            // Return a full HTML page with the error dialog for 404s\n            // Include livereload.js so the page can receive reload messages when the error is fixed\n            let html_page = format!(\n                r#\"<!DOCTYPE html><html><head><title>Zola Build Error</title><script src=\"/livereload.js\"></script></head><body>{html_error}</body></html>\"#\n            );\n            return Response::builder()\n                .header(header::CONTENT_TYPE, \"text/html\")\n                .status(StatusCode::OK)\n                .body(Body::from(html_page))\n                .expect(\"Could not build error response\");\n        }\n    }\n\n    Response::from_parts(parts, Body::from(bytes))\n}\n\nfn in_memory_content(path: &RelativePathBuf, content: &str) -> Response {\n    let content_type = match path.extension() {\n        Some(ext) => match ext {\n            \"xml\" => \"text/xml\",\n            \"json\" => \"application/json\",\n            \"txt\" => \"text/plain\",\n            _ => \"text/html\",\n        },\n        None => \"text/html\",\n    };\n    Response::builder()\n        .header(header::CONTENT_TYPE, content_type)","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/getzola/zola/blob/61d30828217957120309db657950224edf9707e2/src/cmd/serve.rs#L322-L358","documentation":"error_injection_middleware constructs an HTML error page response for build errors with Response::builder().body(...).expect(\"Could not build error response\"). The builder only fails if a header value or body conversion is invalid; with the current static headers this is an invariant panic path in the dev server.","triggerScenarios":"`zola serve` with a build error whose injected html_error content causes the response builder to fail — e.g. after modifying the code to add invalid header values; otherwise unreachable with static headers.","commonSituations":"Customizing the middleware (e.g. adding a content-security-policy header built from dynamic strings) and hitting a hyper HeaderValue parse failure while a rebuild error page is shown.","solutions":["Use HeaderValue::from_static/from_bytes-checked values for any custom header","Construct the response with Response::new and headers_mut() to avoid the fallible builder","Sanitize/validate dynamic header content before injecting it"],"exampleFix":"// before\nreturn Response::builder()\n    .header(header::CONTENT_TYPE, \"text/html\")\n    .status(StatusCode::OK)\n    .body(Body::from(html_page))\n    .expect(\"Could not build error response\");\n// after\nlet mut response = Response::new(Body::from(html_page));\n*response.status_mut() = StatusCode::OK;\nresponse.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static(\"text/html\"));\nreturn response;","handlingStrategy":"fallback","validationCode":"let html_header = http::HeaderValue::from_str(\"text/html\")\n    .expect(\"static header must be valid\");","typeGuard":null,"tryCatchPattern":"match Response::builder()\n    .header(header::CONTENT_TYPE, \"text/html\")\n    .status(StatusCode::OK)\n    .body(Body::from(html_page))\n{\n    Ok(resp) => resp,\n    Err(e) => {\n        eprintln!(\"error page build failed: {}\", e);\n        StatusCode::INTERNAL_SERVER_ERROR.into_response()\n    }\n}","preventionTips":["Build error pages with infallible Response::new + headers_mut()","Validate any dynamic header (e.g. CSP) with HeaderValue::from_str before use","Unit-test error_injection_middleware with a build-error response"],"tags":["rust","axum","http","panic","middleware"],"backgroundTag":"http-response-build-failure","analyzedSha":"61d30828217957120309db657950224edf9707e2","analyzedAt":"2026-09-03T14:39:09.727Z","contentChangedAt":"2026-09-03T14:39:09.727Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}