{"record":{"id":"8244b98d7050760b","repo":"getzola/zola","slug":"could-not-build-method-not-allowed-response","errorCode":null,"errorMessage":"Could not build Method Not Allowed response","messagePattern":"Could not build Method Not Allowed response","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/cmd/serve.rs","lineNumber":369,"sourceCode":"            \"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)\n        .status(StatusCode::OK)\n        .body(Body::from(content.to_owned()))\n        .expect(\"Could not build HTML response\")\n}\n\nfn method_not_allowed() -> Response {\n    Response::builder()\n        .header(header::CONTENT_TYPE, \"text/plain\")\n        .status(StatusCode::METHOD_NOT_ALLOWED)\n        .body(Body::from(METHOD_NOT_ALLOWED_TEXT))\n        .expect(\"Could not build Method Not Allowed response\")\n}\n\nfn io_error(err: std::io::Error) -> Response {\n    match err.kind() {\n        std::io::ErrorKind::NotFound => not_found(),\n        std::io::ErrorKind::PermissionDenied => {\n            Response::builder().status(StatusCode::FORBIDDEN).body(Body::empty()).unwrap()\n        }\n        _ => panic!(\"{}\", err),\n    }\n}\n\nfn not_found() -> Response {\n    let not_found_path = RelativePath::new(\"404.html\");\n    let content = SITE_CONTENT.read().unwrap().get(not_found_path).cloned();\n\n    if let Some(body) = content {\n        return Response::builder()","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/getzola/zola/blob/61d30828217957120309db657950224edf9707e2/src/cmd/serve.rs#L351-L387","documentation":"method_not_allowed builds a static 405 response with fully static headers and body, and .expect panics only if hyper's Response builder rejects them. With the current constants this is an unreachable invariant guard in the dev server.","triggerScenarios":"Issuing a non-GET request to the `zola serve` static server (triggering method_not_allowed) while the builder path fails — only possible if someone makes the headers/body dynamic and invalid.","commonSituations":"Refactoring the constant METHOD_NOT_ALLOWED_TEXT into dynamic content or altering header values with invalid characters, then testing a POST to the dev server.","solutions":["Keep headers/body static; verify constants compile as valid header values","Build via Response::new(Body::from(...)) plus headers_mut() so construction is infallible","Add a unit test exercising method_not_allowed to catch regressions"],"exampleFix":"// before\nResponse::builder()\n    .header(header::CONTENT_TYPE, \"text/plain\")\n    .status(StatusCode::METHOD_NOT_ALLOWED)\n    .body(Body::from(METHOD_NOT_ALLOWED_TEXT))\n    .expect(\"Could not build Method Not Allowed response\")\n// after\nlet mut response = Response::new(Body::from(METHOD_NOT_ALLOWED_TEXT));\n*response.status_mut() = StatusCode::METHOD_NOT_ALLOWED;\nresponse.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static(\"text/plain\"));\nresponse","handlingStrategy":"fallback","validationCode":"let _ = http::HeaderValue::from_static(\"text/plain\"); // compile-time-checked constant","typeGuard":null,"tryCatchPattern":"match Response::builder()\n    .header(header::CONTENT_TYPE, \"text/plain\")\n    .status(StatusCode::METHOD_NOT_ALLOWED)\n    .body(Body::from(METHOD_NOT_ALLOWED_TEXT))\n{\n    Ok(resp) => resp,\n    Err(_) => StatusCode::METHOD_NOT_ALLOWED.into_response(),\n}","preventionTips":["Construct fully static responses with Response::new so no fallible path exists","Add a test asserting POST to the dev server returns 405 without panicking","Keep METHOD_NOT_ALLOWED_TEXT and headers as checked statics"],"tags":["rust","axum","http","panic","dev-server"],"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"}