From fb8733befa0e4f8d027fff8f34e171b6c2a9c479 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 14 Mar 2023 01:30:46 +0100 Subject: [PATCH] fix(backend): return 400 error on bad request, not 500 --- backend/routes/member/create_member.go | 8 ++++---- backend/routes/member/patch_member.go | 6 +++--- backend/routes/user/patch_user.go | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/routes/member/create_member.go b/backend/routes/member/create_member.go index 4b59b1e..824735e 100644 --- a/backend/routes/member/create_member.go +++ b/backend/routes/member/create_member.go @@ -65,15 +65,15 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error } if err := validateSlicePtr("name", &cmr.Names); err != nil { - return err + return *err } if err := validateSlicePtr("pronoun", &cmr.Pronouns); err != nil { - return err + return *err } if err := validateSlicePtr("field", &cmr.Fields); err != nil { - return err + return *err } tx, err := s.DB.Begin(ctx) @@ -175,7 +175,7 @@ func validateSlicePtr[T validator](typ string, slice *[]T) *server.APIError { if s := pronouns.Validate(); s != "" { return &server.APIError{ Code: server.ErrBadRequest, - Details: fmt.Sprintf("%s %d: %s", typ, i, s), + Details: fmt.Sprintf("%s %d: %s", typ, i+1, s), } } } diff --git a/backend/routes/member/patch_member.go b/backend/routes/member/patch_member.go index b909a5b..dd5077e 100644 --- a/backend/routes/member/patch_member.go +++ b/backend/routes/member/patch_member.go @@ -115,15 +115,15 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error { } if err := validateSlicePtr("name", req.Names); err != nil { - return err + return *err } if err := validateSlicePtr("pronoun", req.Pronouns); err != nil { - return err + return *err } if err := validateSlicePtr("field", req.Fields); err != nil { - return err + return *err } // update avatar diff --git a/backend/routes/user/patch_user.go b/backend/routes/user/patch_user.go index a54b68b..486fc7f 100644 --- a/backend/routes/user/patch_user.go +++ b/backend/routes/user/patch_user.go @@ -89,15 +89,15 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { } if err := validateSlicePtr("name", req.Names); err != nil { - return err + return *err } if err := validateSlicePtr("pronoun", req.Pronouns); err != nil { - return err + return *err } if err := validateSlicePtr("field", req.Fields); err != nil { - return err + return *err } // update avatar @@ -250,7 +250,7 @@ func validateSlicePtr[T validator](typ string, slice *[]T) *server.APIError { if s := pronouns.Validate(); s != "" { return &server.APIError{ Code: server.ErrBadRequest, - Details: fmt.Sprintf("%s %d: %s", typ, i, s), + Details: fmt.Sprintf("%s %d: %s", typ, i+1, s), } } }