{"record":{"id":"6bcbd45b26f5da23","repo":"weaviate/weaviate","slug":"err-error-swagger-template-parse-failure","errorCode":null,"errorMessage":"err.Error() (swagger template parse failure)","messagePattern":"err\\.Error\\(\\) \\(swagger template parse failure\\)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"adapters/handlers/rest/swagger_middleware/swagger_middleware.go","lineNumber":55,"sourceCode":"\t\t\tnext.ServeHTTP(w, r)\n\t\t}\n\t})\n}\n\n// renderswagger renders the swagger GUI\nfunc renderSwagger(w http.ResponseWriter, r *http.Request) {\n\tw.Header().Set(\"WWW-Authenticate\", `Basic realm=\"Provide your key and token (as username as password respectively)\"`)\n\n\tuser, password, authOk := r.BasicAuth()\n\tif !authOk {\n\t\thttp.Error(w, \"Not authorized\", http.StatusUnauthorized)\n\t\treturn\n\t}\n\n\tt := template.New(\"Swagger\")\n\tt, err := t.Parse(swaggerTemplate)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\t// Create result string\n\td := templateData{\n\t\tPrefix:   fmt.Sprintf(\"https://cdn.jsdelivr.net/npm/swagger-ui-dist@%s\", swaggerUIVersion),\n\t\tAPIKey:   user,\n\t\tAPIToken: password,\n\t}\n\n\terr = t.ExecuteTemplate(w, \"index\", d)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n}\n\n// tmpl is the page template to render GraphiQL","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/weaviate/weaviate/blob/75aa4b6d11f8818305aafd4440b4e32794f7ca04/adapters/handlers/rest/swagger_middleware/swagger_middleware.go#L37-L73","documentation":"If the embedded swaggerTemplate cannot be parsed by Go's html/template package, renderSwagger writes the raw parse error to the response with HTTP 500. Template parsing happens at request time (not startup), so a malformed swaggerTemplate constant would surface as a per-request 500. In a released build the template is a compile-time constant and valid, so this error almost always indicates a modified/custom binary or templating regression.","triggerScenarios":"A request to the Swagger UI endpoint where t.Parse(swaggerTemplate) fails — only realistically possible when the swaggerTemplate source has been modified (custom fork/patch, build-time injection) and contains invalid template syntax such as unbalanced {{ }} braces or an unknown directive.","commonSituations":"Patching the template to customize the swagger page and introducing a typo like {{ .Prefix } or stray braces; copying the template from a different Go version/context with incompatible syntax; vendoring/fork drift after an upstream update of swaggerUIVersion or the template.","solutions":["Check the error body — it is the raw template parse message naming the line/position of the syntax problem; fix that spot in swaggerTemplate in adapters/handlers/rest/swagger_middleware/swagger_middleware.go.","If running a custom build, revert to the upstream template or validate it: parse it in a small Go test (template.New(\"Swagger\").Parse(tmpl)) before deploying.","Rebuild the binary from unmodified sources to rule out accidental local edits.","If it occurs on an official release image, report upstream with the exact error text."],"exampleFix":"// before (invalid template)\nconst swaggerTemplate = `{{ if .Prefix }...`\n// after (balanced action)\nconst swaggerTemplate = `{{ if .Prefix }}...{{ end }}`","handlingStrategy":"validation","validationCode":"// validate the template parses at build/startup time\ntmpl, err := template.New(\"Swagger\").Parse(swaggerTemplate)\nif err != nil {\n  panic(fmt.Sprintf(\"swagger template invalid: %v\", err))\n}","typeGuard":null,"tryCatchPattern":"// server-side guard if customizing the handler\nt, err := t.Parse(swaggerTemplate)\nif err != nil {\n  http.Error(w, \"swagger template invalid: \"+err.Error(), http.StatusInternalServerError)\n  return\n}","preventionTips":["Keep swaggerTemplate as an unmodified compile-time constant; validate with a unit test that parses and executes it into a buffer.","When customizing, run template.New(...).Parse in a quick test before deploying.","Enable go vet / template tooling to catch malformed {{ }} actions in review.","Pin and review vendored/forked copies of the middleware after upstream updates."],"tags":["go","template","swagger","http-500"],"backgroundTag":"template-parse-error","analyzedSha":"75aa4b6d11f8818305aafd4440b4e32794f7ca04","analyzedAt":"2026-09-04T14:58:20.392Z","contentChangedAt":"2026-09-04T14:58:20.392Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}