From 4cac6ad24ba3e9f1db9d043874075f1b26f2d933 Mon Sep 17 00:00:00 2001 From: Evgenii Shuvalov Date: Fri, 5 Jul 2024 15:34:17 +0300 Subject: [PATCH] iter3 fixes --- go.mod | 2 -- go.sum | 4 ---- internal/server/handlers.go | 25 +++++++++++++++++++------ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index d90b327..51ec017 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,8 @@ module github.com/ex0rcist/metflix go 1.22.4 require ( - github.com/go-chi/chi v1.5.5 github.com/go-chi/chi/v5 v5.1.0 github.com/rs/zerolog v1.33.0 - github.com/satori/go.uuid v1.2.0 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index 8358a24..ea89f77 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= -github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -17,8 +15,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 3e9fc9f..d724f47 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -8,6 +8,7 @@ import ( "github.com/ex0rcist/metflix/internal/metrics" "github.com/ex0rcist/metflix/internal/storage" "github.com/ex0rcist/metflix/internal/validators" + "github.com/rs/zerolog/log" ) type Resource struct { @@ -20,6 +21,15 @@ func NewResource(s storage.Storage) Resource { } } +func writeErrorResponse(w http.ResponseWriter, code int, err error) { + log.Error().Err(err).Msg("") + + w.WriteHeader(code) // only header for now + + // resp := fmt.Sprintf("%d %v", code, err) + // http.Error(w, resp, code) +} + func (r Resource) Homepage(res http.ResponseWriter, _ *http.Request) { body := fmt.Sprintln("mainpage here.") @@ -108,19 +118,19 @@ func (r Resource) ShowMetric(res http.ResponseWriter, req *http.Request) { //При попытке передать запрос без имени метрики возвращать http.StatusNotFound. if err := validators.EnsureNamePresent(metricName); err != nil { - res.WriteHeader(http.StatusNotFound) + writeErrorResponse(res, http.StatusNotFound, err) return } //При попытке передать запрос с некорректным именем метрики возвращать http.StatusBadRequest. if err := validators.ValidateName(metricName); err != nil { - res.WriteHeader(http.StatusBadRequest) + writeErrorResponse(res, http.StatusBadRequest, err) return } //При попытке передать запрос с некорректным типом метрики или значением возвращать http.StatusBadRequest. if err := validators.ValidateKind(metricKind); err != nil { - res.WriteHeader(http.StatusBadRequest) + writeErrorResponse(res, http.StatusBadRequest, err) return } @@ -129,14 +139,17 @@ func (r Resource) ShowMetric(res http.ResponseWriter, req *http.Request) { recordID := storage.CalculateRecordID(metricName, metricKind) record, err := r.storage.Get(recordID) if err != nil { - + writeErrorResponse(res, http.StatusNotFound, err) + return } body := record.Value.String() res.WriteHeader(http.StatusOK) - _, wErr := res.Write([]byte(body)) - if wErr != nil { + _, err = res.Write([]byte(body)) + if err != nil { + writeErrorResponse(res, http.StatusInternalServerError, err) + return } }