| 883 | } |
| 884 | |
| 885 | func (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 |