2015-03-26 16:10:46 +03: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"
|
2015-05-22 05:43:43 +03:00
|
|
|
"reflect"
|
2015-03-26 16:10:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-22 04:25:21 +03:00
|
|
|
ErrorTypeBind = 1 << 31
|
|
|
|
ErrorTypeRender = 1 << 30
|
|
|
|
ErrorTypePrivate = 1 << 0
|
|
|
|
ErrorTypePublic = 1 << 1
|
|
|
|
|
|
|
|
ErrorTypeAny = 0xffffffff
|
|
|
|
ErrorTypeNu = 2
|
2015-03-26 16:10:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Used internally to collect errors that occurred during an http request.
|
|
|
|
type errorMsg struct {
|
2015-05-22 04:43:39 +03:00
|
|
|
Err error `json:"error"`
|
2015-05-22 04:25:21 +03:00
|
|
|
Flags int `json:"-"`
|
|
|
|
Metadata interface{} `json:"meta"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *errorMsg) Type(flags int) *errorMsg {
|
|
|
|
msg.Flags = flags
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *errorMsg) Meta(data interface{}) *errorMsg {
|
|
|
|
msg.Metadata = data
|
|
|
|
return msg
|
2015-03-26 16:10:46 +03:00
|
|
|
}
|
|
|
|
|
2015-05-22 05:43:43 +03:00
|
|
|
func (msg *errorMsg) JSON() interface{} {
|
|
|
|
json := H{}
|
|
|
|
if msg.Metadata != nil {
|
|
|
|
value := reflect.ValueOf(msg.Metadata)
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
return msg.Metadata
|
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range value.MapKeys() {
|
|
|
|
json[key.String()] = value.MapIndex(key).Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := json["error"]; !ok {
|
|
|
|
json["error"] = msg.Error()
|
|
|
|
}
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:43:39 +03:00
|
|
|
func (msg *errorMsg) Error() string {
|
|
|
|
return msg.Err.Error()
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:25:21 +03:00
|
|
|
type errorMsgs []*errorMsg
|
2015-03-26 16:10:46 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func (a errorMsgs) ByType(typ int) errorMsgs {
|
2015-03-26 16:10:46 +03:00
|
|
|
if len(a) == 0 {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
result := make(errorMsgs, 0, len(a))
|
|
|
|
for _, msg := range a {
|
2015-05-12 16:22:13 +03:00
|
|
|
if msg.Flags&typ > 0 {
|
2015-03-26 16:10:46 +03:00
|
|
|
result = append(result, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:43:39 +03:00
|
|
|
func (a errorMsgs) Last() *errorMsg {
|
|
|
|
length := len(a)
|
|
|
|
if length == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a[length-1]
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:34:43 +03:00
|
|
|
func (a errorMsgs) Errors() []string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return []string{}
|
|
|
|
}
|
2015-05-12 16:22:13 +03:00
|
|
|
errorStrings := make([]string, len(a))
|
2015-05-09 04:34:43 +03:00
|
|
|
for i, err := range a {
|
2015-05-22 04:43:39 +03:00
|
|
|
errorStrings[i] = err.Error()
|
2015-05-09 04:34:43 +03:00
|
|
|
}
|
2015-05-12 16:22:13 +03:00
|
|
|
return errorStrings
|
2015-05-09 04:34:43 +03:00
|
|
|
}
|
|
|
|
|
2015-05-22 05:43:43 +03:00
|
|
|
func (a errorMsgs) JSON() interface{} {
|
|
|
|
switch len(a) {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return a.Last().JSON()
|
|
|
|
default:
|
|
|
|
json := make([]interface{}, len(a))
|
|
|
|
for i, err := range a {
|
|
|
|
json[i] = err.JSON()
|
|
|
|
}
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 16:10:46 +03:00
|
|
|
func (a errorMsgs) String() string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
for i, msg := range a {
|
2015-05-22 04:43:39 +03:00
|
|
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n Meta: %v\n", (i + 1), msg.Err, msg.Metadata)
|
2015-03-26 16:10:46 +03:00
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
}
|