Skip to content

Commit

Permalink
Don't panic on ErrEmailInvalid (go-gitea#19441)
Browse files Browse the repository at this point in the history
- Don't panic on `ErrEmailInvalid`, this was caused due that we were
trying to force `ErrEmailCharIsNotSupported` interface, which panics.
- Resolves go-gitea#19397
  • Loading branch information
Gusted authored and AbdulrhmnGhanem committed Aug 23, 2022
1 parent c8ba829 commit 2c81c3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions integrations/api_user_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func TestAPIAddEmail(t *testing.T) {
Primary: false,
},
}, 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) {
Expand Down
13 changes: 10 additions & 3 deletions routers/api/v1/user/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,16 @@ func AddEmail(ctx *context.APIContext) {
if err := user_model.AddEmailAddresses(emails); err != nil {
if user_model.IsErrEmailAlreadyUsed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
} else if user_model.IsErrEmailCharIsNotSupported(err) ||
user_model.IsErrEmailInvalid(err) {
errMsg := fmt.Sprintf("Email address %s invalid", err.(user_model.ErrEmailInvalid).Email)
} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
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)
} else {
ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
Expand Down

0 comments on commit 2c81c3c

Please sign in to comment.