Skip to content

Commit 2c81c3c

Browse files
GustedAbdulrhmnGhanem
Gusted
authored andcommitted
Don't panic on ErrEmailInvalid (go-gitea#19441)
- Don't panic on `ErrEmailInvalid`, this was caused due that we were trying to force `ErrEmailCharIsNotSupported` interface, which panics. - Resolves go-gitea#19397
1 parent c8ba829 commit 2c81c3c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

integrations/api_user_email_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func TestAPIAddEmail(t *testing.T) {
6969
Primary: false,
7070
},
7171
}, emails)
72+
73+
opts = api.CreateEmailOption{
74+
Emails: []string{"notAEmail"},
75+
}
76+
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
77+
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
7278
}
7379

7480
func TestAPIDeleteEmail(t *testing.T) {

routers/api/v1/user/email.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ func AddEmail(ctx *context.APIContext) {
8080
if err := user_model.AddEmailAddresses(emails); err != nil {
8181
if user_model.IsErrEmailAlreadyUsed(err) {
8282
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
83-
} else if user_model.IsErrEmailCharIsNotSupported(err) ||
84-
user_model.IsErrEmailInvalid(err) {
85-
errMsg := fmt.Sprintf("Email address %s invalid", err.(user_model.ErrEmailInvalid).Email)
83+
} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
84+
email := ""
85+
if typedError, ok := err.(user_model.ErrEmailInvalid); ok {
86+
email = typedError.Email
87+
}
88+
if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok {
89+
email = typedError.Email
90+
}
91+
92+
errMsg := fmt.Sprintf("Email address %q invalid", email)
8693
ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
8794
} else {
8895
ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)

0 commit comments

Comments
 (0)