Improved error handling
This commit is contained in:
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"code.vikunja.io/api/routes/crud"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -77,32 +78,7 @@ func userAddOrUpdate(c echo.Context) error {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// Check for user already exists
|
||||
if models.IsErrUsernameExists(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"A user with this username already exists."})
|
||||
}
|
||||
|
||||
// Check for user with that email already exists
|
||||
if models.IsErrUserEmailExists(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"A user with this email address already exists."})
|
||||
}
|
||||
|
||||
// Check for no username provided
|
||||
if models.IsErrNoUsername(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username."})
|
||||
}
|
||||
|
||||
// Check for no username or password provided
|
||||
if models.IsErrNoUsernamePassword(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Please specify a username and a password."})
|
||||
}
|
||||
|
||||
// Check for user does not exist
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, newUser)
|
||||
|
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"code.vikunja.io/api/routes/crud"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -41,15 +42,7 @@ func UserDelete(c echo.Context) error {
|
||||
err = models.DeleteUserByID(userID, &doer)
|
||||
|
||||
if err != nil {
|
||||
if models.IsErrIDCannotBeZero(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Id cannot be 0"})
|
||||
}
|
||||
|
||||
if models.IsErrCannotDeleteLastUser(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Cannot delete last user."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."})
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{"success"})
|
||||
|
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"code.vikunja.io/api/routes/crud"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
@ -31,8 +32,7 @@ func UserList(c echo.Context) error {
|
||||
s := c.QueryParam("s")
|
||||
users, err := models.ListUsers(s)
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "An error occurred.")
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
// Obfuscate the mailadresses
|
||||
|
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"code.vikunja.io/api/routes/crud"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
@ -25,18 +26,12 @@ func UserShow(c echo.Context) error {
|
||||
|
||||
userInfos, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The user does not exist.")
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error getting user infos.")
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error getting current user.")
|
||||
}
|
||||
|
||||
user, err := models.GetUserByID(userInfos.ID)
|
||||
if err != nil {
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The user does not exist.")
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Error getting user infos.")
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, user)
|
||||
|
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"code.vikunja.io/api/routes/crud"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
@ -50,20 +51,12 @@ func UserChangePassword(c echo.Context) error {
|
||||
|
||||
// Check the current password
|
||||
if _, err = models.CheckUserCredentials(&models.UserLogin{Username: doer.Username, Password: newPW.OldPassword}); err != nil {
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The user does not exist.")
|
||||
}
|
||||
return c.JSON(http.StatusUnauthorized, models.Message{"Wrong password."})
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
// Update the password
|
||||
if err = models.UpdateUserPassword(&doer, newPW.NewPassword); err != nil {
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The user does not exist.")
|
||||
}
|
||||
|
||||
models.Log.Error("Error updating a users password, user: %d, err: %s", doer.ID, err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "An error occurred.")
|
||||
return crud.HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{"The password was updated successfully."})
|
||||
|
Reference in New Issue
Block a user