tsweb: add HTTPError.Header (#5251)
The Header field allows the server to specify specific headers to set. Example use case: server returns 429 with the "Retry-After" header set. Signed-off-by: Joe Tsai <joetsai@digital-static.net>pull/5259/head
parent
a794963e2f
commit
57275a4912
|
@ -298,7 +298,14 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.opts.OnError != nil {
|
if h.opts.OnError != nil {
|
||||||
h.opts.OnError(lw, r, hErr)
|
h.opts.OnError(lw, r, hErr)
|
||||||
} else {
|
} 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:
|
case err != nil:
|
||||||
// Handler returned a generic error. Serve an internal server
|
// Handler returned a generic error. Serve an internal server
|
||||||
|
@ -405,9 +412,10 @@ func (l loggingResponseWriter) Flush() {
|
||||||
//
|
//
|
||||||
// It is the error type to be (optionally) used by Handler.ServeHTTPReturn.
|
// It is the error type to be (optionally) used by Handler.ServeHTTPReturn.
|
||||||
type HTTPError struct {
|
type HTTPError struct {
|
||||||
Code int // HTTP response code to send to client; 0 means means 500
|
Code int // HTTP response code to send to client; 0 means means 500
|
||||||
Msg string // Response body to send to client
|
Msg string // Response body to send to client
|
||||||
Err error // Detailed error to log on the server
|
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.
|
// Error implements the error interface.
|
||||||
|
|
Loading…
Reference in New Issue