2015-03-26 13:10:46 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-09 01:34:43 +00:00
|
|
|
ErrorTypePrivate = 1 << iota
|
|
|
|
ErrorTypePublic = 1 << iota
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrorMaskAny = 0xffffffff
|
2015-03-26 13:10:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Used internally to collect errors that occurred during an http request.
|
|
|
|
type errorMsg struct {
|
2015-05-09 01:34:43 +00:00
|
|
|
Error error `json:"error"`
|
|
|
|
Type int `json:"-"`
|
|
|
|
Meta interface{} `json:"meta"`
|
2015-03-26 13:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type errorMsgs []errorMsg
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func (a errorMsgs) ByType(typ int) errorMsgs {
|
2015-03-26 13:10:46 +00:00
|
|
|
if len(a) == 0 {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
result := make(errorMsgs, 0, len(a))
|
|
|
|
for _, msg := range a {
|
|
|
|
if msg.Type&typ > 0 {
|
|
|
|
result = append(result, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-05-09 01:34:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-26 13:10:46 +00:00
|
|
|
func (a errorMsgs) String() string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
for i, msg := range a {
|
2015-05-09 01:34:43 +00:00
|
|
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Error, msg.Meta)
|
2015-03-26 13:10:46 +00:00
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
}
|