mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-22 05:15:33 +03:00
Clean code
This commit is contained in:
parent
12b98bb58a
commit
b43cfc5b39
5 changed files with 26 additions and 30 deletions
|
@ -41,6 +41,23 @@ func (ctx *Context) Query(name string) string {
|
|||
// return ctx.p[name]
|
||||
// }
|
||||
|
||||
// HasError returns true if error occurs in form validation.
|
||||
func (ctx *Context) HasError() bool {
|
||||
hasErr, ok := ctx.Data["HasError"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return hasErr.(bool)
|
||||
}
|
||||
|
||||
// RenderWithErr used for page has form validation but need to prompt error to users.
|
||||
func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = msg
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, tpl, ctx.Data)
|
||||
}
|
||||
|
||||
// Handle handles and logs error by given status.
|
||||
func (ctx *Context) Handle(status int, title string, err error) {
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
|
|
|
@ -20,7 +20,7 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
|
|||
return
|
||||
}
|
||||
|
||||
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
|
||||
if ctx.HasError() {
|
||||
ctx.Render.HTML(200, "repo/create", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
@ -30,10 +30,7 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
|
|||
user, err := models.GetUserById(form.UserId)
|
||||
if err != nil {
|
||||
if err.Error() == models.ErrUserNotExist.Error() {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "User does not exist"
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, "repo/create", ctx.Data)
|
||||
ctx.RenderWithErr("User does not exist", "repo/create", &form)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -48,10 +45,7 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
|
|||
}
|
||||
|
||||
if err.Error() == models.ErrRepoAlreadyExist.Error() {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "Repository name has already been used"
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, "repo/create", ctx.Data)
|
||||
ctx.RenderWithErr("Repository name has already been used", "repo/create", &form)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,5 @@ func Setting(ctx *middleware.Context) {
|
|||
|
||||
ctx.Data["Title"] = ctx.Data["Title"].(string) + " - settings"
|
||||
ctx.Data["IsRepoToolbarSetting"] = true
|
||||
|
||||
ctx.Render.HTML(200, "repo/setting", ctx.Data)
|
||||
}
|
||||
|
|
|
@ -128,9 +128,7 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
|||
}
|
||||
|
||||
if err := models.AddPublicKey(k); err != nil {
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
log.Error("ssh.AddPublicKey: %v", err)
|
||||
ctx.Render.HTML(200, "base/error", ctx.Data)
|
||||
ctx.Handle(200, "ssh.AddPublicKey", err)
|
||||
return
|
||||
} else {
|
||||
ctx.Data["AddSSHKeySuccess"] = true
|
||||
|
@ -140,9 +138,7 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
|||
// List existed SSH keys.
|
||||
keys, err := models.ListPublicKey(ctx.User.Id)
|
||||
if err != nil {
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
log.Error("ssh.ListPublicKey: %v", err)
|
||||
ctx.Render.HTML(200, "base/error", ctx.Data)
|
||||
ctx.Handle(200, "ssh.ListPublicKey", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -82,10 +82,7 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) {
|
|||
user, err := models.LoginUserPlain(form.UserName, form.Password)
|
||||
if err != nil {
|
||||
if err.Error() == models.ErrUserNotExist.Error() {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "Username or password is not correct"
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||
ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -121,7 +118,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
|
|||
auth.AssignForm(form, ctx.Data)
|
||||
}
|
||||
|
||||
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
|
||||
if ctx.HasError() {
|
||||
ctx.Render.HTML(200, "user/signup", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
@ -133,18 +130,11 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
|
|||
}
|
||||
|
||||
if err := models.RegisterUser(u); err != nil {
|
||||
ctx.Data["HasError"] = true
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
|
||||
switch err.Error() {
|
||||
case models.ErrUserAlreadyExist.Error():
|
||||
ctx.Data["Err_Username"] = true
|
||||
ctx.Data["ErrorMsg"] = "Username has been already taken"
|
||||
ctx.Render.HTML(200, "user/signup", ctx.Data)
|
||||
ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
|
||||
case models.ErrEmailAlreadyUsed.Error():
|
||||
ctx.Data["Err_Email"] = true
|
||||
ctx.Data["ErrorMsg"] = "E-mail address has been already used"
|
||||
ctx.Render.HTML(200, "user/signup", ctx.Data)
|
||||
ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
|
||||
default:
|
||||
ctx.Handle(200, "user.SignUp", err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue