mirror of
https://github.com/go-gitea/gitea
synced 2025-07-23 02:38:35 +00:00
Make every not exist error unwrappable to a fs.ErrNotExist (#20891)
A lot of our code is repeatedly testing if individual errors are specific types of Not Exist errors. This is repetitative and unnecesary. `Unwrap() error` provides a common way of labelling an error as a NotExist error and we can/should use this. This PR has chosen to use the common `io/fs` errors e.g. `fs.ErrNotExist` for our errors. This is in some ways not completely correct as these are not filesystem errors but it seems like a reasonable thing to do and would allow us to simplify a lot of our code to `errors.Is(err, fs.ErrNotExist)` instead of `package.IsErr...NotExist(err)` I am open to suggestions to use a different base error - perhaps `models/db.ErrNotExist` if that would be felt to be better. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
@@ -40,6 +40,10 @@ func (err ErrEmailCharIsNotSupported) Error() string {
|
||||
return fmt.Sprintf("e-mail address contains unsupported character [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
func (err ErrEmailCharIsNotSupported) Unwrap() error {
|
||||
return util.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// ErrEmailInvalid represents an error where the email address does not comply with RFC 5322
|
||||
// or has a leading '-' character
|
||||
type ErrEmailInvalid struct {
|
||||
@@ -56,6 +60,10 @@ func (err ErrEmailInvalid) Error() string {
|
||||
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
func (err ErrEmailInvalid) Unwrap() error {
|
||||
return util.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error.
|
||||
type ErrEmailAlreadyUsed struct {
|
||||
Email string
|
||||
@@ -71,6 +79,10 @@ func (err ErrEmailAlreadyUsed) Error() string {
|
||||
return fmt.Sprintf("e-mail already in use [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
func (err ErrEmailAlreadyUsed) Unwrap() error {
|
||||
return util.ErrAlreadyExist
|
||||
}
|
||||
|
||||
// ErrEmailAddressNotExist email address not exist
|
||||
type ErrEmailAddressNotExist struct {
|
||||
Email string
|
||||
@@ -86,6 +98,10 @@ func (err ErrEmailAddressNotExist) Error() string {
|
||||
return fmt.Sprintf("Email address does not exist [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
func (err ErrEmailAddressNotExist) Unwrap() error {
|
||||
return util.ErrNotExist
|
||||
}
|
||||
|
||||
// ErrPrimaryEmailCannotDelete primary email address cannot be deleted
|
||||
type ErrPrimaryEmailCannotDelete struct {
|
||||
Email string
|
||||
@@ -101,6 +117,10 @@ func (err ErrPrimaryEmailCannotDelete) Error() string {
|
||||
return fmt.Sprintf("Primary email address cannot be deleted [email: %s]", err.Email)
|
||||
}
|
||||
|
||||
func (err ErrPrimaryEmailCannotDelete) Unwrap() error {
|
||||
return util.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// EmailAddress is the list of all email addresses of a user. It also contains the
|
||||
// primary email address which is saved in user table.
|
||||
type EmailAddress struct {
|
||||
|
Reference in New Issue
Block a user