Skip to content

Commit

Permalink
fix: add an error code for empty request body
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Oct 28, 2024
1 parent 834d03c commit 28b2682
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func (a *API) adminUserDelete(w http.ResponseWriter, r *http.Request) error {
// ShouldSoftDelete defaults to false
params := &adminUserDeleteParams{}
if err := retrieveRequestParams(r, params); err != nil {
if err.(*HTTPError).ErrorCode == ErrorCodeBadJSON {
if err.(*HTTPError).ErrorCode == ErrorCodeEmptyRequestBody {
// request body is empty so the default behavior should be to hard delete the user
params.ShouldSoftDelete = false
} else {
Expand Down
1 change: 1 addition & 0 deletions internal/api/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (

ErrorCodeValidationFailed ErrorCode = "validation_failed"
ErrorCodeBadJSON ErrorCode = "bad_json"
ErrorCodeEmptyRequestBody ErrorCode = "empty_request_body"
ErrorCodeEmailExists ErrorCode = "email_exists"
ErrorCodePhoneExists ErrorCode = "phone_exists"
ErrorCodeBadJWT ErrorCode = "bad_jwt"
Expand Down
3 changes: 3 additions & 0 deletions internal/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func retrieveRequestParams[A RequestParams](r *http.Request, params *A) error {
if err != nil {
return internalServerError("Could not read body into byte slice").WithInternalError(err)
}
if len(body) == 0 {
return badRequestError(ErrorCodeEmptyRequestBody, "Request body is empty and not a valid JSON")
}
if err := json.Unmarshal(body, params); err != nil {
return badRequestError(ErrorCodeBadJSON, "Could not parse request body as JSON: %v", err)
}
Expand Down

0 comments on commit 28b2682

Please sign in to comment.