Improved error handling
This commit is contained in:
@ -33,50 +33,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||
// Create
|
||||
err = c.CObject.Create(¤tUser)
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The list does not exist.")
|
||||
}
|
||||
if models.IsErrListTitleCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list title.")
|
||||
}
|
||||
if models.IsErrListTaskCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "You must provide at least a list task text.")
|
||||
}
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The user does not exist.")
|
||||
}
|
||||
if models.IsErrNeedToBeListWriter(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to have write access on that list.")
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceNameCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The namespace name cannot be empty.")
|
||||
}
|
||||
|
||||
if models.IsErrTeamNameCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The team name cannot be empty.")
|
||||
}
|
||||
|
||||
if models.IsErrTeamAlreadyHasAccess(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This team already has access.")
|
||||
}
|
||||
if models.IsErrUserIsMemberOfTeam(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This user is already a member of that team.")
|
||||
}
|
||||
|
||||
if models.IsErrUserAlreadyHasAccess(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This user already has access to this list.")
|
||||
}
|
||||
if models.IsErrUserAlreadyHasNamespaceAccess(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This user already has access to this namespace.")
|
||||
}
|
||||
if models.IsErrInvalidUserRight(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The right is invalid.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
return HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusCreated, c.CObject)
|
||||
|
@ -25,36 +25,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||
|
||||
err = c.CObject.Delete()
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
|
||||
if models.IsErrNeedToBeListAdmin(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.")
|
||||
}
|
||||
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.")
|
||||
}
|
||||
if models.IsErrTeamDoesNotHaveAccessToList(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This team does not have access to the list.")
|
||||
}
|
||||
|
||||
if models.IsErrTeamDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "This team does not exist.")
|
||||
}
|
||||
|
||||
if models.IsErrCannotDeleteLastTeamMember(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "You cannot delete the last member of a team.")
|
||||
}
|
||||
|
||||
if models.IsErrUserDoesNotHaveAccessToList(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This user does not have access to the list.")
|
||||
}
|
||||
|
||||
if models.IsErrUserDoesNotHaveAccessToNamespace(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "This user does not have access to the namespace.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
return HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."})
|
||||
|
@ -2,6 +2,8 @@ package crud
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// WebHandler defines the webhandler object
|
||||
@ -12,3 +14,13 @@ type WebHandler struct {
|
||||
models.Rights
|
||||
}
|
||||
}
|
||||
|
||||
// HandleHTTPError does what it says
|
||||
func HandleHTTPError(err error) *echo.HTTPError {
|
||||
if a, has := err.(models.HTTPErrorProcessor); has {
|
||||
errDetails := a.HTTPError()
|
||||
return echo.NewHTTPError(errDetails.Code, errDetails)
|
||||
}
|
||||
models.Log.Error(err.Error())
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
@ -20,17 +20,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||
|
||||
lists, err := c.CObject.ReadAll(¤tUser)
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
|
||||
if models.IsErrNeedToHaveListReadAccess(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to have read access to this list.")
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "This namespace does not exist.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "An error occurred.")
|
||||
return HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, lists)
|
||||
|
@ -17,21 +17,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||
// Get our object
|
||||
err := c.CObject.ReadOne()
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
if models.IsErrTeamDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "An error occurred.")
|
||||
return HandleHTTPError(err)
|
||||
}
|
||||
|
||||
// Check rights
|
||||
|
@ -31,23 +31,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
||||
// Do the update
|
||||
err = c.CObject.Update()
|
||||
if err != nil {
|
||||
models.Log.Error(err.Error())
|
||||
|
||||
if models.IsErrNeedToBeListAdmin(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The namespace does not exist.")
|
||||
}
|
||||
if models.IsErrNamespaceNameCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The namespace name cannot be empty.")
|
||||
}
|
||||
if models.IsErrNamespaceOwnerCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The namespace owner cannot be empty.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
return HandleHTTPError(err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, c.CObject)
|
||||
|
Reference in New Issue
Block a user