generateErrorHTML renders the error page template with the given error information. It returns the generated HTML as a byte slice and a Content-Type string. If there is an error during template execution or if the client does not accept HTML, it falls back to generating a plain text error message w
(err errctx.Error, reqID string, h http.Header)
| 148 | // If there is an error during template execution or if the client does not accept HTML, |
| 149 | // it falls back to generating a plain text error message with the error message and stack trace. |
| 150 | func generateErrorHTML(err errctx.Error, reqID string, h http.Header) ([]byte, string) { |
| 151 | if !strings.Contains(h.Get(httpheaders.Accept), "text/html") { |
| 152 | return generateErrorText(err) |
| 153 | } |
| 154 | |
| 155 | errorChain := unwrapErrorChain(err) |
| 156 | gover, commit := getBuildInfo() |
| 157 | |
| 158 | data := errorPageData{ |
| 159 | StatusCode: err.StatusCode(), |
| 160 | RequestID: reqID, |
| 161 | PublicMessage: err.PublicMessage(), |
| 162 | Version: version.Version, |
| 163 | Commit: commit, |
| 164 | GoVersion: gover, |
| 165 | ErrorChain: errorChain, |
| 166 | } |
| 167 | |
| 168 | var buf bytes.Buffer |
| 169 | if errorPageTemplate.Execute(&buf, data) != nil { |
| 170 | // In case of template execution error, return the error message and stack trace as plain text. |
| 171 | return generateErrorText(err) |
| 172 | } |
| 173 | |
| 174 | return buf.Bytes(), "text/html; charset=utf-8" |
| 175 | } |
| 176 | |
| 177 | func generateErrorText(err errctx.Error) ([]byte, string) { |
| 178 | body := fmt.Appendf([]byte{}, "%s\n\n%s", err.Error(), err.FormatStack()) |
no test coverage detected