package models import "fmt" // ===================== // User Operation Errors // ===================== // ErrUsernameExists represents a "UsernameAlreadyExists" kind of error. type ErrUsernameExists struct { UserID int64 Username string } // IsErrUsernameExists checks if an error is a ErrUsernameExists. func IsErrUsernameExists(err error) bool { _, ok := err.(ErrUsernameExists) return ok } func (err ErrUsernameExists) Error() string { return fmt.Sprintf("a user with this username does already exist [user id: %d, username: %s]", err.UserID, err.Username) } // ErrNoUsername represents a "UsernameAlreadyExists" kind of error. type ErrNoUsername struct { UserID int64 } // IsErrNoUsername checks if an error is a ErrUsernameExists. func IsErrNoUsername(err error) bool { _, ok := err.(ErrNoUsername) return ok } func (err ErrNoUsername) Error() string { return fmt.Sprintf("you need to specify a username [user id: %d]", err.UserID) } // ErrUserDoesNotExist represents a "UsernameAlreadyExists" kind of error. type ErrUserDoesNotExist struct { UserID int64 } // IsErrUserDoesNotExist checks if an error is a ErrUsernameExists. func IsErrUserDoesNotExist(err error) bool { _, ok := err.(ErrUserDoesNotExist) return ok } 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") } // 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") } // ErrBookTitleCannotBeEmpty represents a "ErrBookTitleCannotBeEmpty" kind of error. type ErrBookTitleCannotBeEmpty struct {} // ErrBookTitleCannotBeEmpty checks if an error is a ErrBookTitleCannotBeEmpty. func IsErrBookTitleCannotBeEmpty(err error) bool { _, ok := err.(ErrBookTitleCannotBeEmpty) return ok } 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") }