summaryrefslogtreecommitdiffstats
path: root/modules/context/context.go
diff options
context:
space:
mode:
authorMorgan Bazalgette <git@howl.moe>2018-01-10 22:34:17 +0100
committerLauris BH <lauris@nix.lv>2018-01-10 23:34:17 +0200
commit65861900cda3bb6d9e2aa80b808b0000383c04b3 (patch)
tree8569d93b6ef092b30b35a4d4da906c6b6950e2ee /modules/context/context.go
parent45c264f681e3f7e1a22a191029836a690959aac3 (diff)
downloadgitea-65861900cda3bb6d9e2aa80b808b0000383c04b3.tar.gz
gitea-65861900cda3bb6d9e2aa80b808b0000383c04b3.zip
Handle refactor (#3339)
* Replace all ctx.Handle with ctx.ServerError or ctx.NotFound * Change Handle(403) to NotFound, avoid using macaron's NotFound
Diffstat (limited to 'modules/context/context.go')
-rw-r--r--modules/context/context.go32
1 files changed, 20 insertions, 12 deletions
diff --git a/modules/context/context.go b/modules/context/context.go
index d050d3a938..e69094863d 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -5,7 +5,6 @@
package context
import (
- "fmt"
"html"
"html/template"
"io"
@@ -92,8 +91,8 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}
ctx.HTML(200, tpl)
}
-// Handle handles and logs error by given status.
-func (ctx *Context) Handle(status int, title string, err error) {
+// NotFound displays a 404 (Not Found) page and prints the given error, if any.
+func (ctx *Context) NotFound(title string, err error) {
if err != nil {
log.Error(4, "%s: %v", title, err)
if macaron.Env != macaron.PROD {
@@ -101,13 +100,22 @@ func (ctx *Context) Handle(status int, title string, err error) {
}
}
- switch status {
- case 404:
- ctx.Data["Title"] = "Page Not Found"
- case 500:
- ctx.Data["Title"] = "Internal Server Error"
+ ctx.Data["Title"] = "Page Not Found"
+ ctx.HTML(http.StatusNotFound, base.TplName("status/404"))
+}
+
+// ServerError displays a 500 (Internal Server Error) page and prints the given
+// error, if any.
+func (ctx *Context) ServerError(title string, err error) {
+ if err != nil {
+ log.Error(4, "%s: %v", title, err)
+ if macaron.Env != macaron.PROD {
+ ctx.Data["ErrorMsg"] = err
+ }
}
- ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
+
+ ctx.Data["Title"] = "Internal Server Error"
+ ctx.HTML(404, base.TplName("status/500"))
}
// NotFoundOrServerError use error check function to determine if the error
@@ -115,11 +123,11 @@ func (ctx *Context) Handle(status int, title string, err error) {
// or error context description for logging purpose of 500 server error.
func (ctx *Context) NotFoundOrServerError(title string, errck func(error) bool, err error) {
if errck(err) {
- ctx.Handle(404, title, err)
+ ctx.NotFound(title, err)
return
}
- ctx.Handle(500, title, err)
+ ctx.ServerError(title, err)
}
// HandleText handles HTTP status code
@@ -218,7 +226,7 @@ func Contexter() macaron.Handler {
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
- ctx.Handle(500, "ParseMultipartForm", err)
+ ctx.ServerError("ParseMultipartForm", err)
return
}
}