Added custom error type for ID cannot be 0

This commit is contained in:
konrad 2018-01-24 12:58:00 +01:00 committed by kolaente
parent d309d5b3b6
commit 2e2877156b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
18 changed files with 76 additions and 18 deletions

View File

@ -45,6 +45,7 @@ func TestAddOrUpdateAuthor(t *testing.T) {
// Pass an empty author to see if it fails
_, err = AddOrUpdateAuthor(Author{})
assert.Error(t, err)
assert.True(t, IsErrAuthorCannotBeEmpty(err))
// Update the author
testauthor.ID = author1.ID
@ -62,4 +63,9 @@ func TestAddOrUpdateAuthor(t *testing.T) {
_, exists, err = GetAuthorByID(author1.ID)
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting an author with ID = 0
err = DeleteAuthorByID(0)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

View File

@ -1,7 +1,5 @@
package models
import "fmt"
// AddOrUpdateAuthor adds a new author based on an author struct
func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
@ -9,7 +7,7 @@ func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) {
if author.ID == 0 {
// Check if the author is empty, only insert it if not
if author.Forename == "" && author.Lastname == "" {
return Author{}, fmt.Errorf("Author cannot be empty")
return Author{}, ErrAuthorCannotBeEmpty{}
}
_, err = x.Insert(&author)

View File

@ -1,12 +1,10 @@
package models
import "fmt"
// DeleteAuthorByID deletes an author by its ID
func DeleteAuthorByID(id int64) error {
// Check if the id is 0
if id == 0 {
return fmt.Errorf("ID cannot be 0")
return ErrIDCannotBeZero{}
}
// Delete the author

View File

@ -126,4 +126,9 @@ func TestAddOrUpdateBook(t *testing.T) {
_, exists, err = GetBookByID(book1.ID)
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting one with ID = 0
err = DeleteBookByID(0)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

View File

@ -1,12 +1,10 @@
package models
import "fmt"
// DeleteBookByID deletes a book by its ID
func DeleteBookByID(id int64) error {
// Check if the id is 0
if id == 0 {
return fmt.Errorf("ID cannot be 0")
return ErrIDCannotBeZero{}
}
// Delete the book

View File

@ -51,3 +51,31 @@ func IsErrUserDoesNotExist(err error) bool {
func (err ErrUserDoesNotExist) Error() string {
return fmt.Sprintf("this user does not exist [user id: %d]", err.UserID)
}
// ErrIDCannotBeZero represents a "UsernameAlreadyExists" kind of error.
type ErrIDCannotBeZero struct {}
// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists.
func IsErrIDCannotBeZero(err error) bool {
_, ok := err.(ErrIDCannotBeZero)
return ok
}
func (err ErrIDCannotBeZero) Error() string {
return fmt.Sprintf("ID cannot be 0")
}
// ErrAuthorCannotBeEmpty represents a "AuthorCannotBeEmpty" kind of error.
type ErrAuthorCannotBeEmpty struct {}
// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists.
func IsErrAuthorCannotBeEmpty(err error) bool {
_, ok := err.(ErrAuthorCannotBeEmpty)
return ok
}
func (err ErrAuthorCannotBeEmpty) Error() string {
return fmt.Sprintf("author cannot be empty")
}

View File

@ -85,4 +85,9 @@ func TestAddOrUpdateItem(t *testing.T) {
_, exists, err = GetItemByID(item1.ID)
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting one with ID = 0
err = DeleteItemByID(0)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

View File

@ -1,12 +1,10 @@
package models
import "fmt"
// DeleteItemByID deletes a item by its ID
func DeleteItemByID(id int64) error {
// Check if the id is 0
if id == 0 {
return fmt.Errorf("ID cannot be 0")
return ErrIDCannotBeZero{}
}
// Delete the item

View File

@ -60,4 +60,9 @@ func TestAddOrUpdatePublisher(t *testing.T) {
_, exists, err = GetPublisherByID(publisher1.ID)
assert.NoError(t, err)
assert.False(t, exists)
// Try deleting one with ID = 0
err = DeletePublisherByID(0)
assert.Error(t, err)
assert.True(t, IsErrIDCannotBeZero(err))
}

View File

@ -1,12 +1,10 @@
package models
import "fmt"
// DeletePublisherByID deletes a publisher by its ID
func DeletePublisherByID(id int64) error {
// Check if the id is 0
if id == 0 {
return fmt.Errorf("ID cannot be 0")
return ErrIDCannotBeZero{}
}
// Delete the publisher

View File

@ -1,12 +1,10 @@
package models
import "fmt"
// DeleteUserByID deletes a user by its ID
func DeleteUserByID(id int64) error {
// Check if the id is 0
if id == 0 {
return fmt.Errorf("ID cannot be 0")
return ErrIDCannotBeZero{}
}
// Delete the user

View File

@ -34,6 +34,9 @@ func AuthorDelete(c echo.Context) error {
err = models.DeleteAuthorByID(authorID)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author."})
}

View File

@ -62,6 +62,9 @@ func AuthorAddOrUpdate(c echo.Context) error {
newAuthor, err := models.AddOrUpdateAuthor(*datAuthor)
if err != nil {
if models.IsErrAuthorCannotBeEmpty(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}

View File

@ -34,6 +34,9 @@ func BookDelete(c echo.Context) error {
err = models.DeleteBookByID(bookID)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book."})
}

View File

@ -62,6 +62,9 @@ func BookAddOrUpdate(c echo.Context) error {
newBook, err := models.AddOrUpdateBook(*datBook)
if err != nil {
if models.IsErrAuthorCannotBeEmpty(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}

View File

@ -34,6 +34,9 @@ func ItemDelete(c echo.Context) error {
err = models.DeleteItemByID(itemID)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete item."})
}

View File

@ -34,6 +34,9 @@ func PublisherDelete(c echo.Context) error {
err = models.DeletePublisherByID(publisherID)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete publisher."})
}

View File

@ -39,6 +39,9 @@ func UserDelete(c echo.Context) error {
err = models.DeleteUserByID(userID)
if err != nil {
if models.IsErrIDCannotBeZero(err) {
return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."})
}