Added error type for no item title
This commit is contained in:
parent
2e2877156b
commit
c3cfc73840
|
@ -78,4 +78,17 @@ func (err ErrAuthorCannotBeEmpty) Error() string {
|
||||||
return fmt.Sprintf("author cannot be empty")
|
return fmt.Sprintf("author cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrItemTitleCannotBeEmpty represents a "ErrItemTitleCannotBeEmpty" kind of error.
|
||||||
|
type ErrItemTitleCannotBeEmpty struct {}
|
||||||
|
|
||||||
|
// IsErrItemTitleCannotBeEmpty checks if an error is a ErrItemTitleCannotBeEmpty.
|
||||||
|
func IsErrItemTitleCannotBeEmpty(err error) bool {
|
||||||
|
_, ok := err.(ErrItemTitleCannotBeEmpty)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err ErrItemTitleCannotBeEmpty) Error() string {
|
||||||
|
return fmt.Sprintf("title cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ func TestAddOrUpdateItem(t *testing.T) {
|
||||||
// Pass an empty item to see if it fails
|
// Pass an empty item to see if it fails
|
||||||
_, err = AddOrUpdateItem(Item{})
|
_, err = AddOrUpdateItem(Item{})
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
assert.True(t, IsErrItemTitleCannotBeEmpty(err))
|
||||||
|
|
||||||
// Update the item
|
// Update the item
|
||||||
testitem.ID = item1.ID
|
testitem.ID = item1.ID
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// AddOrUpdateItem adds or updates a item from a item struct
|
// AddOrUpdateItem adds or updates a item from a item struct
|
||||||
func AddOrUpdateItem(item Item) (newItem Item, err error) {
|
func AddOrUpdateItem(item Item) (newItem Item, err error) {
|
||||||
// save the quantity for later use
|
// save the quantity for later use
|
||||||
|
@ -9,7 +7,7 @@ func AddOrUpdateItem(item Item) (newItem Item, err error) {
|
||||||
|
|
||||||
if item.ID == 0 {
|
if item.ID == 0 {
|
||||||
if item.Title == "" { // Only insert it if the title is not empty
|
if item.Title == "" { // Only insert it if the title is not empty
|
||||||
return Item{}, fmt.Errorf("You need at least a title to create an item")
|
return Item{}, ErrItemTitleCannotBeEmpty{}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = x.Insert(&item)
|
_, err = x.Insert(&item)
|
||||||
|
|
|
@ -57,6 +57,9 @@ func ItemAddOrUpdate(c echo.Context) error {
|
||||||
newItem, err := models.AddOrUpdateItem(*datItem)
|
newItem, err := models.AddOrUpdateItem(*datItem)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if models.IsErrItemTitleCannotBeEmpty(err) {
|
||||||
|
return c.JSON(http.StatusInternalServerError, models.Message{"Please provide at least a title for the item."})
|
||||||
|
}
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue