Add errHandler mid ware
This commit is contained in:
parent
47edf7f4f9
commit
56cf05be77
3 changed files with 37 additions and 5 deletions
32
modules/log/error.go
Normal file
32
modules/log/error.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/codegangsta/martini"
|
||||||
|
"github.com/martini-contrib/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrHandler is an interface for custom error handler.
|
||||||
|
type ErrHandler interface {
|
||||||
|
Handle(string, render.Render, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrHandle is a Middleware that maps a ErrHandler service into the Martini handler chain.
|
||||||
|
func ErrHandle() martini.Handler {
|
||||||
|
return func(context martini.Context) {
|
||||||
|
context.MapTo(&errHandler{}, (*ErrHandler)(nil))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type errHandler struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (eh *errHandler) Handle(title string, r render.Render, err error) {
|
||||||
|
Error("%s: %v", title, err)
|
||||||
|
r.HTML(200, "base/error", map[string]interface{}{
|
||||||
|
"ErrorMsg": err,
|
||||||
|
})
|
||||||
|
}
|
|
@ -5,9 +5,10 @@
|
||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/martini-contrib/render"
|
"github.com/martini-contrib/render"
|
||||||
"github.com/martini-contrib/sessions"
|
"github.com/martini-contrib/sessions"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
|
@ -15,7 +16,7 @@ import (
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session, eh log.ErrHandler) {
|
||||||
data["Title"] = "Create repository"
|
data["Title"] = "Create repository"
|
||||||
|
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
|
@ -63,9 +64,7 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data["ErrorMsg"] = err
|
eh.Handle("repo.Create", r, err)
|
||||||
log.Error("repo.Create: %v", err)
|
|
||||||
r.HTML(200, "base/error", data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||||
|
|
1
web.go
1
web.go
|
@ -50,6 +50,7 @@ func runWeb(*cli.Context) {
|
||||||
// Middlewares.
|
// Middlewares.
|
||||||
m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
|
m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
|
||||||
m.Use(base.InitContext())
|
m.Use(base.InitContext())
|
||||||
|
m.Use(log.ErrHandle())
|
||||||
|
|
||||||
// TODO: should use other store because cookie store is not secure.
|
// TODO: should use other store because cookie store is not secure.
|
||||||
store := sessions.NewCookieStore([]byte("secret123"))
|
store := sessions.NewCookieStore([]byte("secret123"))
|
||||||
|
|
Loading…
Reference in a new issue