From ca60dd2d734147b440a8ce8ec7018577df29bb78 Mon Sep 17 00:00:00 2001 From: Richard87 Date: Thu, 24 Oct 2024 13:17:26 +0200 Subject: [PATCH] fix: dont parse empty strings --- handler.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/handler.go b/handler.go index 0d419b7..138f3cf 100644 --- a/handler.go +++ b/handler.go @@ -50,11 +50,11 @@ func NewBackendController() RouteMapper { logDebugHeaders(r) - errCode := r.Header.Get(CodeHeader) - code, err := strconv.Atoi(errCode) + statusCode := r.Header.Get(CodeHeader) + code, err := parseErrorCode(statusCode) if err != nil { code = 404 - logger.Warn().Err(err).Str("code", errCode).Msg("unexpected error reading return code") + logger.Warn().Err(err).Int("code", code).Msgf("unexpected error reading return code: %s", statusCode) } scode := strconv.Itoa(code) w.WriteHeader(code) @@ -94,6 +94,19 @@ func NewBackendController() RouteMapper { } } +func parseErrorCode(errCode string) (int, error) { + if errCode == "" { + return 404, nil + } + + parsedCode, err := strconv.Atoi(errCode) + if err != nil { + return 404, err + } + + return parsedCode, nil +} + func getHtmlFile(namespace string) string { if namespace == "" { return "default.html"