Handles unique constraint error on user creation
This commit is contained in:
parent
971ce6e2a4
commit
1563210586
1 changed files with 7 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/log"
|
"github.com/gofiber/fiber/v2/log"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultUserError = fiber.NewError(fiber.ErrInternalServerError.Code, "Could not create user")
|
var defaultUserError = fiber.NewError(fiber.ErrInternalServerError.Code, "Could not create user")
|
||||||
|
|
@ -35,8 +36,13 @@ func (h *Handler) createUser(c *fiber.Ctx) error {
|
||||||
|
|
||||||
_, insertErr := h.db.Exec("INSERT INTO user (id, first_name, email) VALUES (?, ?, ?)", user.ID, user.FirstName, user.Email)
|
_, insertErr := h.db.Exec("INSERT INTO user (id, first_name, email) VALUES (?, ?, ?)", user.ID, user.FirstName, user.Email)
|
||||||
|
|
||||||
if insertErr != nil {
|
if sqliteErr, ok := insertErr.(sqlite3.Error); ok {
|
||||||
log.Warn(insertErr)
|
log.Warn(insertErr)
|
||||||
|
|
||||||
|
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "User with email already exists")
|
||||||
|
}
|
||||||
|
|
||||||
return defaultUserError
|
return defaultUserError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue