From 1ce17cce764b5c10feeba5a92586be9a45173e26 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 14 Mar 2014 01:59:07 -0400 Subject: [PATCH] Add log.handle --- modules/log/log.go | 6 ++++-- routers/repo/repo.go | 10 +++------- routers/repo/single.go | 4 +--- routers/user/setting.go | 12 +++-------- routers/user/user.go | 20 +++++-------------- .../{base/error.tmpl => status/200.tmpl} | 0 6 files changed, 16 insertions(+), 36 deletions(-) rename templates/{base/error.tmpl => status/200.tmpl} (100%) diff --git a/modules/log/log.go b/modules/log/log.go index f71be7c133..b3a3e0a63c 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -6,6 +6,8 @@ package log import ( + "fmt" + "github.com/martini-contrib/render" "github.com/gogits/logs" @@ -40,8 +42,8 @@ func Critical(format string, v ...interface{}) { logger.Critical(format, v...) } -func Handle(status int, title, tmpl string, data base.TmplData, r render.Render, err error) { +func Handle(status int, title string, data base.TmplData, r render.Render, err error) { data["ErrorMsg"] = err Error("%s: %v", title, err) - r.HTML(status, tmpl, data) + r.HTML(status, fmt.Sprintf("status/%d", status), data) } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 706b4f937d..b25ce1899c 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -64,7 +64,7 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b return } - log.Handle(200, "repo.Create", "base/error", data, r, err) + log.Handle(200, "repo.Create", data, r, err) } func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { @@ -76,9 +76,7 @@ func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data b } if err := models.DeleteRepository(form.UserId, form.RepoId, form.UserName); err != nil { - data["ErrorMsg"] = err - log.Error("repo.Delete: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "repo.Delete", data, r, err) return } @@ -95,9 +93,7 @@ func List(req *http.Request, r render.Render, data base.TmplData, session sessio data["Title"] = "Repositories" repos, err := models.GetRepositories(u) if err != nil { - data["ErrorMsg"] = err - log.Error("repo.List: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "repo.List", data, r, err) return } diff --git a/routers/repo/single.go b/routers/repo/single.go index 1d5e601aa3..489666f406 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -16,9 +16,7 @@ func Single(params martini.Params, r render.Render, data base.TmplData) { files, err := models.GetReposFiles(params["username"], params["reponame"], "HEAD", "/") if err != nil { - data["ErrorMsg"] = err - log.Error("repo.List: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "repo.Single", data, r, err) return } diff --git a/routers/user/setting.go b/routers/user/setting.go index 08879ae4a7..b01d27d845 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -40,9 +40,7 @@ func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, r user.Avatar = base.EncodeMd5(form.Avatar) user.AvatarEmail = form.Avatar if err := models.UpdateUser(user); err != nil { - data["ErrorMsg"] = err - log.Error("setting.Setting: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "setting.Setting", data, r, err) return } @@ -62,9 +60,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl user := auth.SignedInUser(session) newUser := &models.User{Passwd: form.NewPasswd} if err := newUser.EncodePasswd(); err != nil { - data["ErrorMsg"] = err - log.Error("setting.UpdatePasswd: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "setting.SettingPassword", data, r, err) return } @@ -77,9 +73,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl } else { user.Passwd = newUser.Passwd if err := models.UpdateUser(user); err != nil { - data["ErrorMsg"] = err - log.Error("setting.UpdatePasswd: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "setting.SettingPassword", data, r, err) return } data["IsSuccess"] = true diff --git a/routers/user/user.go b/routers/user/user.go index 2ce158b641..6095b53808 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -22,9 +22,7 @@ func Dashboard(r render.Render, data base.TmplData, session sessions.Session) { data["PageIsUserDashboard"] = true repos, err := models.GetRepositories(&models.User{Id: auth.SignedInId(session)}) if err != nil { - data["ErrorMsg"] = err - log.Error("dashboard: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "user.Dashboard", data, r, err) return } data["MyRepos"] = repos @@ -37,9 +35,7 @@ func Profile(params martini.Params, r render.Render, data base.TmplData, session // TODO: Need to check view self or others. user, err := models.GetUserByName(params["username"]) if err != nil { - data["ErrorMsg"] = err - log.Error("user.Profile: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "user.Profile", data, r, err) return } @@ -70,9 +66,7 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render return } - data["ErrorMsg"] = err - log.Error("user.SignIn: %v", err) - r.HTML(200, "base/error", data) + log.Handle(200, "user.SignIn", data, r, err) return } @@ -129,9 +123,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren data["ErrorMsg"] = "E-mail address has been already used" r.HTML(200, "user/signup", data) default: - data["ErrorMsg"] = err - log.Error("user.SignUp: %v", data) - r.HTML(200, "base/error", nil) + log.Handle(200, "user.SignUp", data, r, err) } return } @@ -155,9 +147,7 @@ func Delete(data base.TmplData, req *http.Request, session sessions.Session, r r case models.ErrUserOwnRepos.Error(): data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first." default: - data["ErrorMsg"] = err - log.Error("user.Delete: %v", data) - r.HTML(200, "base/error", nil) + log.Handle(200, "user.Delete", data, r, err) return } } diff --git a/templates/base/error.tmpl b/templates/status/200.tmpl similarity index 100% rename from templates/base/error.tmpl rename to templates/status/200.tmpl