From 26130a5df6756673bcbb52c8b8783d77070405e8 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 30 Mar 2015 20:35:57 +0800 Subject: [PATCH] fix #1073 --- error.go | 18 +++++++++--------- router.go | 14 +++----------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/error.go b/error.go index 71be6916..6db4a5a8 100644 --- a/error.go +++ b/error.go @@ -439,24 +439,26 @@ func exception(errcode string, ctx *context.Context) { if err != nil { code = 503 } - ctx.ResponseWriter.WriteHeader(code) if h, ok := ErrorMaps[errcode]; ok { - executeError(h, ctx) + executeError(h, ctx, code) return } else if h, ok := ErrorMaps["503"]; ok { - executeError(h, ctx) + executeError(h, ctx, code) return } else { + ctx.ResponseWriter.WriteHeader(code) ctx.WriteString(errcode) } } -func executeError(err *errorInfo, ctx *context.Context) { +func executeError(err *errorInfo, ctx *context.Context, code int) { if err.errorType == errorTypeHandler { + ctx.ResponseWriter.WriteHeader(code) err.handler(ctx.ResponseWriter, ctx.Request) return } if err.errorType == errorTypeController { + ctx.Output.SetStatus(code) //Invoke the request handler vc := reflect.New(err.controllerType) execController, ok := vc.Interface().(ControllerInterface) @@ -476,11 +478,9 @@ func executeError(err *errorInfo, ctx *context.Context) { method.Call(in) //render template - if ctx.Output.Status == 0 { - if AutoRender { - if err := execController.Render(); err != nil { - panic(err) - } + if AutoRender { + if err := execController.Render(); err != nil { + panic(err) } } diff --git a/router.go b/router.go index c1bda637..e9f45615 100644 --- a/router.go +++ b/router.go @@ -862,8 +862,8 @@ func (p *ControllerRegistor) recoverPanic(context *beecontext.Context) { panic(err) } else { if ErrorsShow { - if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok { - executeError(handler, context) + if _, ok := ErrorMaps[fmt.Sprint(err)]; ok { + exception(fmt.Sprint(err), context) return } } @@ -886,15 +886,7 @@ func (p *ControllerRegistor) recoverPanic(context *beecontext.Context) { } else { // in production model show all infomation if ErrorsShow { - if handler, ok := ErrorMaps[fmt.Sprint(err)]; ok { - executeError(handler, context) - return - } else if handler, ok := ErrorMaps["503"]; ok { - executeError(handler, context) - return - } else { - context.WriteString(fmt.Sprint(err)) - } + exception(fmt.Sprint(err), context) } else { Critical("the request url is ", context.Input.Url()) Critical("Handler crashed with error", err)