mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
caddyhttp: Default to error status if found in context
This is just a convenience if using a static_response handler in an error route, by setting the default status code to the same one as the error status.
This commit is contained in:
parent
2d1f7b9da8
commit
7b0962ba4d
1 changed files with 9 additions and 1 deletions
|
@ -121,8 +121,16 @@ func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Hand
|
|||
w.Header()["Content-Type"] = nil
|
||||
}
|
||||
|
||||
// get the status code
|
||||
// get the status code; if this handler exists in an error route,
|
||||
// use the recommended status code as the default; otherwise 200
|
||||
statusCode := http.StatusOK
|
||||
if reqErr, ok := r.Context().Value(ErrorCtxKey).(error); ok {
|
||||
if handlerErr, ok := reqErr.(HandlerError); ok {
|
||||
if handlerErr.StatusCode > 0 {
|
||||
statusCode = handlerErr.StatusCode
|
||||
}
|
||||
}
|
||||
}
|
||||
if codeStr := s.StatusCode.String(); codeStr != "" {
|
||||
intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, ""))
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue