fix(backend): return 400 error on bad request, not 500

This commit is contained in:
Sam 2023-03-14 01:30:46 +01:00
parent 1ebbeebdcb
commit fb8733befa
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
3 changed files with 11 additions and 11 deletions

View file

@ -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),
}
}
}

View file

@ -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

View file

@ -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),
}
}
}