MCPcopy
hub / github.com/caddyserver/caddy / handleError

Method handleError

admin.go:885–919  ·  admin.go::adminHandler.handleError
(w http.ResponseWriter, r *http.Request, err error)

Source from the content-addressed store, hash-verified

883}
884
885func (h adminHandler) handleError(w http.ResponseWriter, r *http.Request, err error) {
886 if err == nil {
887 return
888 }
889 if err == errInternalRedir {
890 h.serveHTTP(w, r)
891 return
892 }
893
894 apiErr, ok := err.(APIError)
895 if !ok {
896 apiErr = APIError{
897 HTTPStatus: http.StatusInternalServerError,
898 Err: err,
899 }
900 }
901 if apiErr.HTTPStatus == 0 {
902 apiErr.HTTPStatus = http.StatusInternalServerError
903 }
904 if apiErr.Message == "" && apiErr.Err != nil {
905 apiErr.Message = apiErr.Err.Error()
906 }
907
908 Log().Named("admin.api").Error("request error",
909 zap.Error(err),
910 zap.Int("status_code", apiErr.HTTPStatus),
911 )
912
913 w.Header().Set("Content-Type", "application/json")
914 w.WriteHeader(apiErr.HTTPStatus)
915 encErr := json.NewEncoder(w).Encode(apiErr)
916 if encErr != nil {
917 Log().Named("admin.api").Error("failed to encode error response", zap.Error(encErr))
918 }
919}
920
921// checkHost returns a handler that wraps next such that
922// it will only be called if the request's Host header matches

Callers 3

newAdminHandlerMethod · 0.95
serveHTTPMethod · 0.95

Calls 8

serveHTTPMethod · 0.95
LogFunction · 0.85
NewEncoderMethod · 0.65
ErrorMethod · 0.45
IntMethod · 0.45
SetMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by 1