{"record":{"id":"2d40220912ad7e1f","repo":"ory/hydra","slug":"failed-to-render-template-s","errorCode":null,"errorMessage":"Failed to render template: %s","messagePattern":"Failed to render template: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"cmd/cmd_perform_device_flow.go","lineNumber":168,"sourceCode":"\tmux *http.ServeMux\n}\n\nfunc (s *deviceSrv) ServeHTTP(w http.ResponseWriter, r *http.Request) {\n\ts.mux.ServeHTTP(w, r)\n}\n\nfunc (s *deviceSrv) GETdevice(w http.ResponseWriter, r *http.Request) {\n\tif r.URL.Query().Get(\"device_challenge\") == \"\" {\n\t\thttp.Error(w, \"device_challenge is required\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\terr := userCodeTemplate.Execute(w, userCodeData{\n\t\tUserCode:        r.URL.Query().Get(\"user_code\"),\n\t\tDeviceChallenge: r.URL.Query().Get(\"device_challenge\"),\n\t})\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to render template: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n}\n\nfunc (s *deviceSrv) POSTdevice(w http.ResponseWriter, r *http.Request) {\n\tif err := r.ParseForm(); err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to parse form: %s\", err), http.StatusBadRequest)\n\t\treturn\n\t}\n\tuserCode, challenge := r.FormValue(\"user_code\"), r.FormValue(\"device_challenge\")\n\tif userCode == \"\" {\n\t\thttp.Error(w, \"user_code is required\", http.StatusBadRequest)\n\t\treturn\n\t}\n\tif challenge == \"\" {\n\t\thttp.Error(w, \"device_challenge is required\", http.StatusBadRequest)\n\t\treturn\n\t}","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cmd_perform_device_flow.go#L150-L186","documentation":"GETdevice renders the user-code HTML page via userCodeTemplate.Execute directly into the ResponseWriter. If template execution fails (e.g. a custom template referenced a missing field), the handler writes a 500 with the template error message.","triggerScenarios":"userCodeTemplate.Execute returns an error while writing the user_code/device_challenge data — typically a broken or incompatible custom template passed in place of the built-in one.","commonSituations":"Overriding the default template with one referencing fields not in userCodeData; template syntax errors introduced by customization; ResponseWriter failing mid-write (client disconnect).","solutions":["Restore or fix the custom template so it only references fields on userCodeData (user_code, device_challenge)","Validate the template parses (template.Must) at startup to fail fast","Check server logs for the exact template error printed in the 500 body","Fall back to the built-in template to verify the handler itself is healthy"],"exampleFix":"// before\ntmpl := template.Must(template.New(\"usercode\").Parse(userTpl))\n// after\ntmpl, err := template.New(\"usercode\").Parse(userTpl)\nif err != nil {\n\tlog.Fatalf(\"invalid user code template: %v\", err)\n}","handlingStrategy":"validation","validationCode":"// validate template at startup, not at request time\nvar userCodeTemplate = template.Must(template.New(\"usercode\").Parse(defaultTpl))","typeGuard":null,"tryCatchPattern":"if err != nil {\n\tlog.Printf(\"user code template render failed: %v\", err)\n\thttp.Error(w, \"failed to render page\", http.StatusInternalServerError)\n\treturn\n}","preventionTips":["Use template.Must at startup so broken templates fail fast","Only reference fields defined on userCodeData in custom templates","Render into a buffer in tests to catch template errors before shipping","Pin template customizations to the CLI version"],"tags":["http","device-flow","template"],"backgroundTag":"template-render-failed","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}