Skip to content

Commit

Permalink
add ErrorPage PipeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dworld committed Nov 8, 2014
1 parent 601a27a commit b16621e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* context use ResponseWriter instead of http.ResponseWriter OK
* panic if write header after body OK
* add Session OK
* add PipeHandler ErrorPage
* add PipeHandler ErrorPage OK
* test and benchmark
* Example
7 changes: 1 addition & 6 deletions context_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ func (ctx *Context) NotModified() {
ctx.WriteHeader(304)
}

func (ctx *Context) NotFound(message string) {
func (ctx *Context) NotFound() {
ctx.WriteHeader(404)
_, err := ctx.Write([]byte(message))
if err != nil {
ctx.Logger.Errorf(err.Error())
return
}
}

func (ctx *Context) Unauthorized() {
Expand Down
19 changes: 19 additions & 0 deletions pipe_error_page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xingyun

import "net/http"

func (s *Server) GetErrorPagePipeHandler() PipeHandler {
return PipeHandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
s.Logger.Tracef("enter error page handler")
defer s.Logger.Tracef("exit error page handler")

next(w, r)
ctx := GetContext(r)
status := ctx.ResponseWriter.Status()
if status >= 400 && status <= 599 {
if s.ErrorPageHandler != nil {
s.ErrorPageHandler.ServeContext(ctx)
}
}
})
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewServer(config *Config) *Server {
server.PanicHandler = DefaultPanicHandler
server.pipes = map[string]*Pipe{}
server.DefaultPipeHandlers = []PipeHandler{
server.GetErrorPagePipeHandler(),
server.GetLogPipeHandler(),
server.GetRecoverPipeHandler(),
server.GetStaticPipeHandler(),
Expand Down

0 comments on commit b16621e

Please sign in to comment.