tsweb: add HTTPError.Header
The Header field allows the server to specify specific headers to set.
Example use case: server returns 429 with the "Retry-After" header set.
@@ -298,7 +298,14 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.opts.OnError != nil {
h.opts.OnError(lw, r, hErr)
} else {
- http.Error(lw, hErr.Msg, msg.Code)
+ // Default headers set by http.Error.
+ lw.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ lw.Header().Set("X-Content-Type-Options", "nosniff")
+ for k, vs := range hErr.Header {
+ lw.Header()[k] = vs
+ }
+ lw.WriteHeader(msg.Code)
+ fmt.Fprintln(lw, hErr.Msg)
}
case err != nil:
// Handler returned a generic error. Serve an internal server
@@ -408,6 +415,7 @@ type HTTPError struct {
Code int // HTTP response code to send to client; 0 means means 500
Msg string // Response body to send to client
Err error // Detailed error to log on the server
+ Header http.Header // Optional set of HTTP headers to set in the response
}
// Error implements the error interface.
