From b5362d58b9f04972fc664a4ce7d7b4b6a8fb0d33 Mon Sep 17 00:00:00 2001 From: Taras Madan Date: Wed, 24 Jul 2024 11:17:03 +0200 Subject: dashboard/app/api.go: handleAPI can return less 5xx errors --- dashboard/app/handler.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'dashboard/app/handler.go') diff --git a/dashboard/app/handler.go b/dashboard/app/handler.go index 145f79ef9..91c051e2b 100644 --- a/dashboard/app/handler.go +++ b/dashboard/app/handler.go @@ -60,16 +60,7 @@ func handleContext(fn contextHandler) http.Handler { return } - status := http.StatusInternalServerError - logf := log.Errorf - var clientError *ErrClient - if errors.As(err, &clientError) { - // We don't log these as errors because they can be provoked - // by invalid user requests, so we don't wan't to pollute error log. - logf = log.Warningf - status = clientError.HTTPStatus() - } - logf(c, "%v", err) + status := logErrorPrepareStatus(c, err) w.WriteHeader(status) if err1 := templates.ExecuteTemplate(w, "error.html", data); err1 != nil { combinedError := fmt.Sprintf("got err \"%v\" processing ExecuteTemplate() for err \"%v\"", err1, err) @@ -79,6 +70,20 @@ func handleContext(fn contextHandler) http.Handler { }) } +func logErrorPrepareStatus(c context.Context, err error) int { + status := http.StatusInternalServerError + logf := log.Errorf + var clientError *ErrClient + if errors.As(err, &clientError) { + // We don't log these as errors because they can be provoked + // by invalid user requests, so we don't wan't to pollute error log. + logf = log.Warningf + status = clientError.HTTPStatus() + } + logf(c, "%v", err) + return status +} + func isRobot(r *http.Request) bool { userAgent := strings.ToLower(strings.Join(r.Header["User-Agent"], " ")) if strings.HasPrefix(userAgent, "curl") || -- cgit mrf-deployment