forked from gitea/gitea
1
0
Fork 0

Don't panic on `ErrEmailInvalid` (#19441) (#19442)

- Backport #19441
  - Don't panic on `ErrEmailInvalid`, this was caused due that we were trying to force `ErrEmailCharIsNotSupported` interface, which panics.
  - Resolves #19397

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Gusted 2022-04-20 22:24:07 +00:00 committed by GitHub
parent a785c46ca8
commit 5863f7e048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -69,6 +69,12 @@ func TestAPIAddEmail(t *testing.T) {
Primary: false, Primary: false,
}, },
}, emails) }, emails)
opts = api.CreateEmailOption{
Emails: []string{"notAEmail"},
}
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
} }
func TestAPIDeleteEmail(t *testing.T) { func TestAPIDeleteEmail(t *testing.T) {

View File

@ -80,9 +80,16 @@ func AddEmail(ctx *context.APIContext) {
if err := user_model.AddEmailAddresses(emails); err != nil { if err := user_model.AddEmailAddresses(emails); err != nil {
if user_model.IsErrEmailAlreadyUsed(err) { if user_model.IsErrEmailAlreadyUsed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email) ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
} else if user_model.IsErrEmailCharIsNotSupported(err) || } else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
user_model.IsErrEmailInvalid(err) { email := ""
errMsg := fmt.Sprintf("Email address %s invalid", err.(user_model.ErrEmailInvalid).Email) if typedError, ok := err.(user_model.ErrEmailInvalid); ok {
email = typedError.Email
}
if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok {
email = typedError.Email
}
errMsg := fmt.Sprintf("Email address %q invalid", email)
ctx.Error(http.StatusUnprocessableEntity, "", errMsg) ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
} else { } else {
ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err) ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)