Skip to content

Commit

Permalink
📋 impl (controllers.api-user): The user registration handler now can …
Browse files Browse the repository at this point in the history
…return a error response data in JSON
  • Loading branch information
kevinmarquesp committed Jul 20, 2024
1 parent be9bc8f commit 9daf3da
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/controllers/api_register_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"strings"

"github.com/charmbracelet/log"
"github.com/kevinmarquesp/go-postr/internal/models"
"github.com/kevinmarquesp/go-postr/internal/services"
)

Expand All @@ -30,7 +30,12 @@ func (au ApiUserHandler) RegisterNewUserWithCredentials(w http.ResponseWriter, r
Confirmation string `json:"confirmation"`
}

json.NewDecoder(r.Body).Decode(&props)
err := json.NewDecoder(r.Body).Decode(&props)
if err != nil {
models.WriteHttpJsonError(w, http.StatusBadRequest, err,
"controllers.api-user", "The request body is invalid.")
return
}

createdUser, err := au.AuthenticationService.AuthenticateWithCredentials(
strings.Trim(props.Name, " "),
Expand All @@ -40,8 +45,8 @@ func (au ApiUserHandler) RegisterNewUserWithCredentials(w http.ResponseWriter, r
strings.Trim(props.Confirmation, " "),
)
if err != nil {
// TODO: Create a function to format the error in a JSON HTTP response.
log.Error("Could not register the new user for some reason.", "error", err)
models.WriteHttpJsonError(w, http.StatusInternalServerError, err,
"services.authentication", "Could not register the request user to the database.")
return
}

Expand Down

0 comments on commit 9daf3da

Please sign in to comment.