From 9854e8e32bba4ba0a9aa4252508d9a4602a38f54 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 20 Apr 2022 19:14:51 +0200 Subject: [PATCH] Don't panic on `ErrEmailInvalid` (#19441) - Backport #19441 - Don't panic on `ErrEmailInvalid`, this was caused due that we were trying to force `ErrEmailCharIsNotSupported` interface, which panics. - Resolves #19397 --- integrations/api_user_email_test.go | 6 ++++++ routers/api/v1/user/email.go | 13 ++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/integrations/api_user_email_test.go b/integrations/api_user_email_test.go index 9d2b7485d8526..08d236df30e52 100644 --- a/integrations/api_user_email_test.go +++ b/integrations/api_user_email_test.go @@ -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) { diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index ed79723c60f6b..7423bfbf2a994 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -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)