Merge branch 'develop'
This commit is contained in:
commit
f99a2ac7cc
33
gin.go
33
gin.go
@ -1,8 +1,11 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
@ -22,17 +25,19 @@ type (
|
|||||||
|
|
||||||
// Used internally to collect a error ocurred during a http request.
|
// Used internally to collect a error ocurred during a http request.
|
||||||
ErrorMsg struct {
|
ErrorMsg struct {
|
||||||
Message string `json:"msg"`
|
Err string `json:"error"`
|
||||||
Meta interface{} `json:"meta"`
|
Meta interface{} `json:"meta"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorMsgs []ErrorMsg
|
||||||
|
|
||||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
// 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.
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
Context struct {
|
Context struct {
|
||||||
Req *http.Request
|
Req *http.Request
|
||||||
Writer http.ResponseWriter
|
Writer http.ResponseWriter
|
||||||
Keys map[string]interface{}
|
Keys map[string]interface{}
|
||||||
Errors []ErrorMsg
|
Errors ErrorMsgs
|
||||||
Params httprouter.Params
|
Params httprouter.Params
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
engine *Engine
|
engine *Engine
|
||||||
@ -57,6 +62,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.
|
// Returns a new blank Engine instance without any middleware attached.
|
||||||
// The most basic configuration
|
// The most basic configuration
|
||||||
func New() *Engine {
|
func New() *Engine {
|
||||||
@ -240,11 +254,20 @@ 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.
|
// 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{}) {
|
func (c *Context) Error(err error, meta interface{}) {
|
||||||
c.Errors = append(c.Errors, ErrorMsg{
|
c.Errors = append(c.Errors, ErrorMsg{
|
||||||
Message: err.Error(),
|
Err: err.Error(),
|
||||||
Meta: meta,
|
Meta: meta,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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********/
|
/******** METADATA MANAGEMENT********/
|
||||||
/************************************/
|
/************************************/
|
||||||
@ -344,8 +367,10 @@ 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"
|
// Writes the given string into the response body and sets the Content-Type to "text/plain"
|
||||||
func (c *Context) String(code int, msg string) {
|
func (c *Context) String(code int, msg string) {
|
||||||
c.Writer.Header().Set("Content-Type", "text/plain")
|
if code >= 0 {
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
|
}
|
||||||
|
c.Writer.Header().Set("Content-Type", "text/plain")
|
||||||
c.Writer.Write([]byte(msg))
|
c.Writer.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ func Logger() HandlerFunc {
|
|||||||
// Calculate resolution time
|
// Calculate resolution time
|
||||||
log.Printf("%s in %v", c.Req.RequestURI, time.Since(t))
|
log.Printf("%s in %v", c.Req.RequestURI, time.Since(t))
|
||||||
if len(c.Errors) > 0 {
|
if len(c.Errors) > 0 {
|
||||||
fmt.Println(c.Errors)
|
fmt.Println(c.Errors.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ func Recovery() HandlerFunc {
|
|||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if len(c.Errors) > 0 {
|
if len(c.Errors) > 0 {
|
||||||
log.Println(c.Errors)
|
log.Println(c.Errors.String())
|
||||||
}
|
}
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
stack := stack(3)
|
stack := stack(3)
|
||||||
|
Loading…
Reference in New Issue
Block a user