From 068bfc942ec3fed48aec02b1c78122d363b2a11d Mon Sep 17 00:00:00 2001 From: konrad Date: Thu, 25 Jan 2018 12:23:51 +0100 Subject: [PATCH] Beautified error messages --- models/books_add_update.go | 2 +- models/error.go | 58 +++++++++++++++-------------- routes/api/v1/authors_add_update.go | 2 +- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/models/books_add_update.go b/models/books_add_update.go index 1b7e341..bbbf10b 100644 --- a/models/books_add_update.go +++ b/models/books_add_update.go @@ -19,7 +19,7 @@ sie in die Datenbank eingetragen und mit dem Buch verknüpft. func AddOrUpdateBook(book Book) (newBook Book, err error) { // Check if we have at least a booktitle when we're inserting a new book - if book.Title == ""{ + if book.Title == "" { return Book{}, ErrBookTitleCannotBeEmpty{} } diff --git a/models/error.go b/models/error.go index 2d6bc79..89653a2 100644 --- a/models/error.go +++ b/models/error.go @@ -37,10 +37,10 @@ func (err ErrNoUsername) Error() string { return fmt.Sprintf("you need to specify a username [user id: %d]", err.UserID) } -// ErrNoUsernamePassword represents a "UsernameAlreadyExists" kind of error. -type ErrNoUsernamePassword struct {} +// ErrNoUsernamePassword represents a "NoUsernamePassword" kind of error. +type ErrNoUsernamePassword struct{} -// IsErrNoUsernamePassword checks if an error is a ErrUsernameExists. +// IsErrNoUsernamePassword checks if an error is a ErrNoUsernamePassword. func IsErrNoUsernamePassword(err error) bool { _, ok := err.(ErrNoUsernamePassword) return ok @@ -50,12 +50,12 @@ func (err ErrNoUsernamePassword) Error() string { return fmt.Sprintf("you need to specify a username and a password") } -// ErrUserDoesNotExist represents a "UsernameAlreadyExists" kind of error. +// ErrUserDoesNotExist represents a "UserDoesNotExist" kind of error. type ErrUserDoesNotExist struct { UserID int64 } -// IsErrUserDoesNotExist checks if an error is a ErrUsernameExists. +// IsErrUserDoesNotExist checks if an error is a ErrUserDoesNotExist. func IsErrUserDoesNotExist(err error) bool { _, ok := err.(ErrUserDoesNotExist) return ok @@ -65,10 +65,27 @@ 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 {} +// ErrCouldNotGetUserID represents a "ErrCouldNotGetUserID" kind of error. +type ErrCouldNotGetUserID struct{} -// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists. +// IsErrCouldNotGetUserID checks if an error is a ErrCouldNotGetUserID. +func IsErrCouldNotGetUserID(err error) bool { + _, ok := err.(ErrCouldNotGetUserID) + return ok +} + +func (err ErrCouldNotGetUserID) Error() string { + return fmt.Sprintf("could not get user ID") +} + +// =================== +// Empty things errors +// =================== + +// ErrIDCannotBeZero represents a "IDCannotBeZero" kind of error. Used if an ID (of something, not defined) is 0 where it should not. +type ErrIDCannotBeZero struct{} + +// IsErrIDCannotBeZero checks if an error is a ErrIDCannotBeZero. func IsErrIDCannotBeZero(err error) bool { _, ok := err.(ErrIDCannotBeZero) return ok @@ -79,9 +96,9 @@ func (err ErrIDCannotBeZero) Error() string { } // ErrAuthorCannotBeEmpty represents a "AuthorCannotBeEmpty" kind of error. -type ErrAuthorCannotBeEmpty struct {} +type ErrAuthorCannotBeEmpty struct{} -// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists. +// IsErrAuthorCannotBeEmpty checks if an error is a ErrAuthorCannotBeEmpty. func IsErrAuthorCannotBeEmpty(err error) bool { _, ok := err.(ErrAuthorCannotBeEmpty) return ok @@ -92,7 +109,7 @@ func (err ErrAuthorCannotBeEmpty) Error() string { } // ErrItemTitleCannotBeEmpty represents a "ErrItemTitleCannotBeEmpty" kind of error. -type ErrItemTitleCannotBeEmpty struct {} +type ErrItemTitleCannotBeEmpty struct{} // IsErrItemTitleCannotBeEmpty checks if an error is a ErrItemTitleCannotBeEmpty. func IsErrItemTitleCannotBeEmpty(err error) bool { @@ -105,9 +122,9 @@ func (err ErrItemTitleCannotBeEmpty) Error() string { } // ErrBookTitleCannotBeEmpty represents a "ErrBookTitleCannotBeEmpty" kind of error. -type ErrBookTitleCannotBeEmpty struct {} +type ErrBookTitleCannotBeEmpty struct{} -// ErrBookTitleCannotBeEmpty checks if an error is a ErrBookTitleCannotBeEmpty. +// IsErrBookTitleCannotBeEmpty checks if an error is a ErrBookTitleCannotBeEmpty. func IsErrBookTitleCannotBeEmpty(err error) bool { _, ok := err.(ErrBookTitleCannotBeEmpty) return ok @@ -117,21 +134,8 @@ func (err ErrBookTitleCannotBeEmpty) Error() string { return fmt.Sprintf("the book should at least have a title") } -// ErrCouldNotGetUserID represents a "ErrCouldNotGetUserID" kind of error. -type ErrCouldNotGetUserID struct {} - -// ErrBookTitleCannotBeEmpty checks if an error is a ErrBookTitleCannotBeEmpty. -func IsErrCouldNotGetUserID(err error) bool { - _, ok := err.(ErrCouldNotGetUserID) - return ok -} - -func (err ErrCouldNotGetUserID) Error() string { - return fmt.Sprintf("could not get user ID") -} - // ErrNoPublisherName represents a "ErrNoPublisherName" kind of error. -type ErrNoPublisherName struct {} +type ErrNoPublisherName struct{} // IsErrNoPublisherName checks if an error is a ErrNoPublisherName. func IsErrNoPublisherName(err error) bool { diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 3cbfec1..b540c57 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -63,7 +63,7 @@ func AuthorAddOrUpdate(c echo.Context) error { if err != nil { if models.IsErrAuthorCannotBeEmpty(err) { - return c.JSON(http.StatusBadRequest, models.Message{"Id cannot be 0"}) + return c.JSON(http.StatusBadRequest, models.Message{"Please provide at least a name and a lastname."}) } return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) }