From a2a7f5948244339f298e90fff68c84aecb67adca Mon Sep 17 00:00:00 2001 From: Paul Balogh Date: Mon, 22 Jan 2024 10:28:58 -0600 Subject: [PATCH] Address linter issues Signed-off-by: Paul Balogh --- api/api.go | 61 ++++++++++++++++++++++++++++++++-------------------- api/place.go | 6 +++--- cmd/serve.go | 1 + 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/api/api.go b/api/api.go index 1430056..e603525 100644 --- a/api/api.go +++ b/api/api.go @@ -9,6 +9,8 @@ import ( "strings" "time" + "github.com/pkg/errors" + "github.com/google/uuid" "github.com/gorilla/mux" "github.com/sirupsen/logrus" @@ -69,6 +71,7 @@ func (a *API) handler(f func(*app.Context, http.ResponseWriter, *http.Request) e ctx := a.App.NewContext().WithRemoteAddress(a.addressForRequest(r)).WithTraceID(traceID) defer func() { + //nolint: forcetypeassert statusCode := w.(*statusCodeRecorder).StatusCode if statusCode == 0 { statusCode = 200 @@ -93,29 +96,15 @@ func (a *API) handler(f func(*app.Context, http.ResponseWriter, *http.Request) e w.Header().Set("Content-Type", "application/json") if err := f(ctx, w, r); err != nil { - if verr, ok := err.(*app.ValidationError); ok { - data, err := json.Marshal(verr) - if err == nil { - w.WriteHeader(http.StatusBadRequest) - _, err = w.Write(data) - } - - if err != nil { - ctx.Logger.Error(err) - http.Error(w, "internal server error", http.StatusInternalServerError) - } - } else if uerr, ok := err.(*app.UserError); ok { - data, err := json.Marshal(uerr) - if err == nil { - w.WriteHeader(uerr.StatusCode) - _, err = w.Write(data) - } - - if err != nil { - ctx.Logger.Error(err) - http.Error(w, "internal server error", http.StatusInternalServerError) - } - } else { + var verr *app.ValidationError + var uerr *app.UserError + + switch { + case errors.As(err, &verr): + handleValidationError(ctx, w, verr) + case errors.As(err, &uerr): + handleUserError(ctx, w, uerr) + default: ctx.Logger.Error(err) http.Error(w, "internal server error", http.StatusInternalServerError) } @@ -123,6 +112,32 @@ func (a *API) handler(f func(*app.Context, http.ResponseWriter, *http.Request) e }) } +func handleValidationError(ctx *app.Context, w http.ResponseWriter, verr *app.ValidationError) { + data, err := json.Marshal(verr) + if err == nil { + w.WriteHeader(http.StatusBadRequest) + _, err = w.Write(data) + } + + if err != nil { + ctx.Logger.Error(err) + http.Error(w, "internal server error", http.StatusInternalServerError) + } +} + +func handleUserError(ctx *app.Context, w http.ResponseWriter, uerr *app.UserError) { + data, err := json.Marshal(uerr) + if err == nil { + w.WriteHeader(uerr.StatusCode) + _, err = w.Write(data) + } + + if err != nil { + ctx.Logger.Error(err) + http.Error(w, "internal server error", http.StatusInternalServerError) + } +} + func (a *API) helloHandler(ctx *app.Context, w http.ResponseWriter, _ *http.Request) error { _, err := w.Write([]byte( fmt.Sprintf(`{"hello":"world","remote_address":%q,"trace_id":%q}`, diff --git a/api/place.go b/api/place.go index 6ca94aa..cd87c48 100644 --- a/api/place.go +++ b/api/place.go @@ -50,7 +50,7 @@ func (a *API) createPlace(ctx *app.Context, w http.ResponseWriter, r *http.Reque return err } - if err := json.Unmarshal(body, &input); err != nil { + if err = json.Unmarshal(body, &input); err != nil { return err } @@ -61,7 +61,7 @@ func (a *API) createPlace(ctx *app.Context, w http.ResponseWriter, r *http.Reque Longitude: input.Longitude, } - if err := ctx.CreatePlace(place); err != nil { + if err = ctx.CreatePlace(place); err != nil { return err } @@ -110,7 +110,7 @@ func (a *API) updatePlaceByID(ctx *app.Context, w http.ResponseWriter, r *http.R return err } - if err := json.Unmarshal(body, &input); err != nil { + if err = json.Unmarshal(body, &input); err != nil { return err } diff --git a/cmd/serve.go b/cmd/serve.go index b5b21cf..2aa09b7 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -36,6 +36,7 @@ func serveAPI(ctx context.Context, api *api.API) { done := make(chan struct{}) go func() { <-ctx.Done() + //nolint:contextcheck if err := s.Shutdown(context.Background()); err != nil { logrus.Error(err) }