mirror of
https://github.com/gin-gonic/gin.git
synced 2025-08-24 07:55:38 +08:00
Better debug logging + unit tests
This commit is contained in:
29
errors.go
29
errors.go
@ -10,16 +10,19 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorTypeInternal = 1 << iota
|
||||
ErrorTypeExternal = 1 << iota
|
||||
ErrorTypeAll = 0xffffffff
|
||||
ErrorTypePrivate = 1 << iota
|
||||
ErrorTypePublic = 1 << iota
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorMaskAny = 0xffffffff
|
||||
)
|
||||
|
||||
// Used internally to collect errors that occurred during an http request.
|
||||
type errorMsg struct {
|
||||
Err string `json:"error"`
|
||||
Type int `json:"-"`
|
||||
Meta interface{} `json:"meta"`
|
||||
Error error `json:"error"`
|
||||
Type int `json:"-"`
|
||||
Meta interface{} `json:"meta"`
|
||||
}
|
||||
|
||||
type errorMsgs []errorMsg
|
||||
@ -37,14 +40,24 @@ func (a errorMsgs) ByType(typ int) errorMsgs {
|
||||
return result
|
||||
}
|
||||
|
||||
func (a errorMsgs) Errors() []string {
|
||||
if len(a) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
errors := make([]string, len(a))
|
||||
for i, err := range a {
|
||||
errors[i] = err.Error.Error()
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
func (a errorMsgs) String() string {
|
||||
if len(a) == 0 {
|
||||
return ""
|
||||
}
|
||||
var buffer bytes.Buffer
|
||||
for i, msg := range a {
|
||||
text := fmt.Sprintf("Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Err, msg.Meta)
|
||||
buffer.WriteString(text)
|
||||
fmt.Fprintf(&buffer, "Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Error, msg.Meta)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
Reference in New Issue
Block a user