Beautified error messages
the build failed Details

This commit is contained in:
konrad 2018-01-25 12:23:51 +01:00 committed by kolaente
parent a734f21ac2
commit 068bfc942e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 33 additions and 29 deletions

View File

@ -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{}
}

View File

@ -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 {

View File

@ -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"})
}