Skip to content

Commit

Permalink
Address linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Balogh <[email protected]>
  • Loading branch information
javaducky committed Jan 22, 2024
1 parent 18f1716 commit a2a7f59
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
61 changes: 38 additions & 23 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
Expand All @@ -93,36 +96,48 @@ 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)
}
}
})
}

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}`,
Expand Down
6 changes: 3 additions & 3 deletions api/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a2a7f59

Please sign in to comment.