1
1
mirror of https://github.com/go-gitea/gitea synced 2025-08-13 04:58:19 +00:00

Handle refactor (#3339)

* Replace all ctx.Handle with ctx.ServerError or ctx.NotFound

* Change Handle(403) to NotFound, avoid using macaron's NotFound
This commit is contained in:
Morgan Bazalgette
2018-01-10 22:34:17 +01:00
committed by Lauris BH
parent 45c264f681
commit 65861900cd
48 changed files with 622 additions and 610 deletions

View File

@@ -73,7 +73,7 @@ func handleUsernameChange(ctx *context.Context, newName string) {
ctx.Flash.Error(ctx.Tr("user.newName_pattern_not_allowed"))
ctx.Redirect(setting.AppSubURL + "/user/settings")
default:
ctx.Handle(500, "ChangeUserName", err)
ctx.ServerError("ChangeUserName", err)
}
return
}
@@ -111,7 +111,7 @@ func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
ctx.Redirect(setting.AppSubURL + "/user/settings")
return
}
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
return
}
@@ -202,7 +202,7 @@ func SettingsSecurity(ctx *context.Context) {
if models.IsErrTwoFactorNotEnrolled(err) {
enrolled = false
} else {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
}
@@ -232,12 +232,12 @@ func SettingsSecurityPost(ctx *context.Context, form auth.ChangePasswordForm) {
ctx.User.Passwd = form.Password
var err error
if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
return
}
ctx.User.HashPassword()
if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
return
}
log.Trace("User password updated: %s", ctx.User.Name)
@@ -254,7 +254,7 @@ func SettingsEmails(ctx *context.Context) {
emails, err := models.GetEmailAddresses(ctx.User.ID)
if err != nil {
ctx.Handle(500, "GetEmailAddresses", err)
ctx.ServerError("GetEmailAddresses", err)
return
}
ctx.Data["Emails"] = emails
@@ -270,7 +270,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
// Make emailaddress primary.
if ctx.Query("_method") == "PRIMARY" {
if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
ctx.Handle(500, "MakeEmailPrimary", err)
ctx.ServerError("MakeEmailPrimary", err)
return
}
@@ -282,7 +282,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
// Add Email address.
emails, err := models.GetEmailAddresses(ctx.User.ID)
if err != nil {
ctx.Handle(500, "GetEmailAddresses", err)
ctx.ServerError("GetEmailAddresses", err)
return
}
ctx.Data["Emails"] = emails
@@ -302,7 +302,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsEmails, &form)
return
}
ctx.Handle(500, "AddEmailAddress", err)
ctx.ServerError("AddEmailAddress", err)
return
}
@@ -325,7 +325,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
// DeleteEmail response for delete user's email
func DeleteEmail(ctx *context.Context) {
if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
ctx.Handle(500, "DeleteEmail", err)
ctx.ServerError("DeleteEmail", err)
return
}
log.Trace("Email address deleted: %s", ctx.User.Name)
@@ -344,14 +344,14 @@ func SettingsKeys(ctx *context.Context) {
keys, err := models.ListPublicKeys(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListPublicKeys", err)
ctx.ServerError("ListPublicKeys", err)
return
}
ctx.Data["Keys"] = keys
gpgkeys, err := models.ListGPGKeys(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListGPGKeys", err)
ctx.ServerError("ListGPGKeys", err)
return
}
ctx.Data["GPGKeys"] = gpgkeys
@@ -366,14 +366,14 @@ func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
keys, err := models.ListPublicKeys(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListPublicKeys", err)
ctx.ServerError("ListPublicKeys", err)
return
}
ctx.Data["Keys"] = keys
gpgkeys, err := models.ListGPGKeys(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListGPGKeys", err)
ctx.ServerError("ListGPGKeys", err)
return
}
ctx.Data["GPGKeys"] = gpgkeys
@@ -398,7 +398,7 @@ func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
ctx.Data["Err_Content"] = true
ctx.RenderWithErr(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form)
default:
ctx.Handle(500, "AddPublicKey", err)
ctx.ServerError("AddPublicKey", err)
}
return
}
@@ -428,7 +428,7 @@ func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
ctx.Data["Err_Title"] = true
ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsKeys, &form)
default:
ctx.Handle(500, "AddPublicKey", err)
ctx.ServerError("AddPublicKey", err)
}
return
}
@@ -474,7 +474,7 @@ func SettingsApplications(ctx *context.Context) {
tokens, err := models.ListAccessTokens(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListAccessTokens", err)
ctx.ServerError("ListAccessTokens", err)
return
}
ctx.Data["Tokens"] = tokens
@@ -490,7 +490,7 @@ func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm
if ctx.HasError() {
tokens, err := models.ListAccessTokens(ctx.User.ID)
if err != nil {
ctx.Handle(500, "ListAccessTokens", err)
ctx.ServerError("ListAccessTokens", err)
return
}
ctx.Data["Tokens"] = tokens
@@ -503,7 +503,7 @@ func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm
Name: form.Name,
}
if err := models.NewAccessToken(t); err != nil {
ctx.Handle(500, "NewAccessToken", err)
ctx.ServerError("NewAccessToken", err)
return
}
@@ -533,17 +533,17 @@ func SettingsTwoFactorRegenerateScratch(ctx *context.Context) {
t, err := models.GetTwoFactorByUID(ctx.User.ID)
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err = t.GenerateScratchToken(); err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err = models.UpdateTwoFactor(t); err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
@@ -558,12 +558,12 @@ func SettingsTwoFactorDisable(ctx *context.Context) {
t, err := models.GetTwoFactorByUID(ctx.User.ID)
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err = models.DeleteTwoFactorByID(t.ID, ctx.User.ID); err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
@@ -585,7 +585,7 @@ func twofaGenerateSecretAndQr(ctx *context.Context) bool {
AccountName: ctx.User.Name,
})
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return false
}
}
@@ -593,13 +593,13 @@ func twofaGenerateSecretAndQr(ctx *context.Context) bool {
ctx.Data["TwofaSecret"] = otpKey.Secret()
img, err := otpKey.Image(320, 240)
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return false
}
var imgBytes bytes.Buffer
if err = png.Encode(&imgBytes, img); err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return false
}
@@ -617,11 +617,11 @@ func SettingsTwoFactorEnroll(ctx *context.Context) {
t, err := models.GetTwoFactorByUID(ctx.User.ID)
if t != nil {
// already enrolled
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
@@ -640,11 +640,11 @@ func SettingsTwoFactorEnrollPost(ctx *context.Context, form auth.TwoFactorAuthFo
t, err := models.GetTwoFactorByUID(ctx.User.ID)
if t != nil {
// already enrolled
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
@@ -671,17 +671,17 @@ func SettingsTwoFactorEnrollPost(ctx *context.Context, form auth.TwoFactorAuthFo
}
err = t.SetSecret(secret)
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
err = t.GenerateScratchToken()
if err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
if err = models.NewTwoFactor(t); err != nil {
ctx.Handle(500, "SettingsTwoFactor", err)
ctx.ServerError("SettingsTwoFactor", err)
return
}
@@ -698,7 +698,7 @@ func SettingsAccountLinks(ctx *context.Context) {
accountLinks, err := models.ListAccountLinks(ctx.User)
if err != nil {
ctx.Handle(500, "ListAccountLinks", err)
ctx.ServerError("ListAccountLinks", err)
return
}
@@ -745,7 +745,7 @@ func SettingsDelete(ctx *context.Context) {
if models.IsErrUserNotExist(err) {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
} else {
ctx.Handle(500, "UserSignIn", err)
ctx.ServerError("UserSignIn", err)
}
return
}
@@ -759,7 +759,7 @@ func SettingsDelete(ctx *context.Context) {
ctx.Flash.Error(ctx.Tr("form.still_has_org"))
ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
default:
ctx.Handle(500, "DeleteUser", err)
ctx.ServerError("DeleteUser", err)
}
} else {
log.Trace("Account deleted: %s", ctx.User.Name)
@@ -777,7 +777,7 @@ func SettingsOrganization(ctx *context.Context) {
ctx.Data["PageIsSettingsOrganization"] = true
orgs, err := models.GetOrgsByUserID(ctx.User.ID, ctx.IsSigned)
if err != nil {
ctx.Handle(500, "GetOrgsByUserID", err)
ctx.ServerError("GetOrgsByUserID", err)
return
}
ctx.Data["Orgs"] = orgs
@@ -792,7 +792,7 @@ func SettingsRepos(ctx *context.Context) {
var err error
if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
ctx.Handle(500, "GetRepositories", err)
ctx.ServerError("GetRepositories", err)
return
}
repos := ctxUser.Repos
@@ -801,12 +801,12 @@ func SettingsRepos(ctx *context.Context) {
if repos[i].IsFork {
err := repos[i].GetBaseRepo()
if err != nil {
ctx.Handle(500, "GetBaseRepo", err)
ctx.ServerError("GetBaseRepo", err)
return
}
err = repos[i].BaseRepo.GetOwner()
if err != nil {
ctx.Handle(500, "GetOwner", err)
ctx.ServerError("GetOwner", err)
return
}
}