From 2abbc4ad1dc5b5b606861501c1f2d3195a570295 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 2 Jul 2014 01:47:32 +0200 Subject: [PATCH 1/4] Setting response headers before calling WriteHeader in context.String() --- gin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin.go b/gin.go index fc5da8c6..66530d26 100644 --- a/gin.go +++ b/gin.go @@ -344,8 +344,8 @@ func (c *Context) HTML(code int, name string, data interface{}) { // Writes the given string into the response body and sets the Content-Type to "text/plain" func (c *Context) String(code int, msg string) { - c.Writer.Header().Set("Content-Type", "text/plain") c.Writer.WriteHeader(code) + c.Writer.Header().Set("Content-Type", "text/plain") c.Writer.Write([]byte(msg)) } From 4b62957d853a3241384e3902e9c35334459c8a4e Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 2 Jul 2014 01:50:33 +0200 Subject: [PATCH 2/4] Do not update header when status code is less than 1 --- gin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gin.go b/gin.go index 66530d26..9c294203 100644 --- a/gin.go +++ b/gin.go @@ -344,7 +344,9 @@ func (c *Context) HTML(code int, name string, data interface{}) { // Writes the given string into the response body and sets the Content-Type to "text/plain" func (c *Context) String(code int, msg string) { - c.Writer.WriteHeader(code) + if code >= 0 { + c.Writer.WriteHeader(code) + } c.Writer.Header().Set("Content-Type", "text/plain") c.Writer.Write([]byte(msg)) } From f72fcdf5ba80ea028d2b9ec77a201295cc08d1a8 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 2 Jul 2014 02:31:11 +0200 Subject: [PATCH 3/4] Improves error log formatting --- gin.go | 23 ++++++++++++++++++----- logger.go | 2 +- recovery.go | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/gin.go b/gin.go index 9c294203..19a1ffa3 100644 --- a/gin.go +++ b/gin.go @@ -1,8 +1,10 @@ package gin import ( + "bytes" "encoding/json" "encoding/xml" + "fmt" "github.com/julienschmidt/httprouter" "html/template" "log" @@ -22,17 +24,19 @@ type ( // Used internally to collect a error ocurred during a http request. ErrorMsg struct { - Message string `json:"msg"` - Meta interface{} `json:"meta"` + Err string `json:"error"` + Meta interface{} `json:"meta"` } + ErrorMsgs []ErrorMsg + // Context is the most important part of gin. It allows us to pass variables between middleware, // manage the flow, validate the JSON of a request and render a JSON response for example. Context struct { Req *http.Request Writer http.ResponseWriter Keys map[string]interface{} - Errors []ErrorMsg + Errors ErrorMsgs Params httprouter.Params handlers []HandlerFunc engine *Engine @@ -57,6 +61,15 @@ type ( } ) +func (a ErrorMsgs) String() string { + var buffer bytes.Buffer + for i, msg := range a { + text := fmt.Sprintf("Error #%02d: %s \n Meta: %v\n\n", (i + 1), msg.Err, msg.Meta) + buffer.WriteString(text) + } + return buffer.String() +} + // Returns a new blank Engine instance without any middleware attached. // The most basic configuration func New() *Engine { @@ -240,8 +253,8 @@ func (c *Context) Fail(code int, err error) { // A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response. func (c *Context) Error(err error, meta interface{}) { c.Errors = append(c.Errors, ErrorMsg{ - Message: err.Error(), - Meta: meta, + Err: err.Error(), + Meta: meta, }) } diff --git a/logger.go b/logger.go index dbd3c1ea..7f0abdb4 100644 --- a/logger.go +++ b/logger.go @@ -29,7 +29,7 @@ func Logger() HandlerFunc { // Calculate resolution time log.Printf("%s in %v", c.Req.RequestURI, time.Since(t)) if len(c.Errors) > 0 { - fmt.Println(c.Errors) + fmt.Println(c.Errors.String()) } } } diff --git a/recovery.go b/recovery.go index cbde0e8a..15066c8e 100644 --- a/recovery.go +++ b/recovery.go @@ -83,7 +83,7 @@ func Recovery() HandlerFunc { return func(c *Context) { defer func() { if len(c.Errors) > 0 { - log.Println(c.Errors) + log.Println(c.Errors.String()) } if err := recover(); err != nil { stack := stack(3) From b8798ec5f70a00f84f0ed64fab461a06b9d8919e Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 2 Jul 2014 02:32:44 +0200 Subject: [PATCH 4/4] Adds context.LastError() --- gin.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gin.go b/gin.go index 19a1ffa3..a74bcd8a 100644 --- a/gin.go +++ b/gin.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "encoding/xml" + "errors" "fmt" "github.com/julienschmidt/httprouter" "html/template" @@ -258,6 +259,15 @@ func (c *Context) Error(err error, meta interface{}) { }) } +func (c *Context) LastError() error { + s := len(c.Errors) + if s > 0 { + return errors.New(c.Errors[s-1].Err) + } else { + return nil + } +} + /************************************/ /******** METADATA MANAGEMENT********/ /************************************/