diff --git a/models/error.go b/models/error.go index e54993b..5a6ff22 100644 --- a/models/error.go +++ b/models/error.go @@ -117,3 +117,15 @@ func (err ErrCouldNotGetUserID) Error() string { return fmt.Sprintf("could not get user ID") } +// ErrNoPublisherName represents a "ErrNoPublisherName" kind of error. +type ErrNoPublisherName struct {} + +// IsErrNoPublisherName checks if an error is a ErrNoPublisherName. +func IsErrNoPublisherName(err error) bool { + _, ok := err.(ErrNoPublisherName) + return ok +} + +func (err ErrNoPublisherName) Error() string { + return fmt.Sprintf("you need at least a name to insert a new publisher") +} diff --git a/models/publisher_test.go b/models/publisher_test.go index 794fd78..9e0d911 100644 --- a/models/publisher_test.go +++ b/models/publisher_test.go @@ -44,6 +44,7 @@ func TestAddOrUpdatePublisher(t *testing.T) { // Pass an empty publisher to see if it fails _, err = AddOrUpdatePublisher(Publisher{}) assert.Error(t, err) + assert.True(t, IsErrNoPublisherName(err)) // Update the publisher testpublisher.ID = publisher1.ID diff --git a/models/publishers_add_update.go b/models/publishers_add_update.go index 525469d..a767a91 100644 --- a/models/publishers_add_update.go +++ b/models/publishers_add_update.go @@ -1,12 +1,10 @@ package models -import "fmt" - // AddOrUpdatePublisher adds or updates a publisher from a publisher struct func AddOrUpdatePublisher(publisher Publisher) (newPublisher Publisher, err error) { if publisher.ID == 0 { if publisher.Name == "" { // Only insert it if the name is not empty - return Publisher{}, fmt.Errorf("You need at least a name to insert a new publisher") + return Publisher{}, ErrNoPublisherName{} } _, err = x.Insert(&publisher) diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index 115afe2..9233e8f 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -67,7 +67,11 @@ func BookAddOrUpdate(c echo.Context) error { } if models.IsErrBookTitleCannotBeEmpty(err) { - return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a Title for the book."}) + return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a title for the book."}) + } + + if models.IsErrNoPublisherName(err) { + return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a name to insert a new publisher."}) } return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) diff --git a/routes/api/v1/publishers_add_update.go b/routes/api/v1/publishers_add_update.go index b52ba26..db3d2fe 100644 --- a/routes/api/v1/publishers_add_update.go +++ b/routes/api/v1/publishers_add_update.go @@ -58,6 +58,10 @@ func PublisherAddOrUpdate(c echo.Context) error { newPublisher, err := models.AddOrUpdatePublisher(*datPublisher) if err != nil { + if models.IsErrNoPublisherName(err) { + return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a name to insert a new publisher."}) + } + return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) }