mtfosbot/vendor/github.com/gin-gonic/gin/errors.go

158 lines
3.2 KiB
Go
Raw Permalink Normal View History

2018-08-14 09:25:34 +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"
"reflect"
2018-11-16 03:10:19 +00:00
"github.com/gin-gonic/gin/json"
2018-08-14 09:25:34 +00:00
)
type ErrorType uint64
const (
ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
ErrorTypePrivate ErrorType = 1 << 0
ErrorTypePublic ErrorType = 1 << 1
ErrorTypeAny ErrorType = 1<<64 - 1
ErrorTypeNu = 2
)
2018-11-16 03:10:19 +00:00
type Error struct {
Err error
Type ErrorType
Meta interface{}
}
2018-08-14 09:25:34 +00:00
2018-11-16 03:10:19 +00:00
type errorMsgs []*Error
2018-08-14 09:25:34 +00:00
var _ error = &Error{}
func (msg *Error) SetType(flags ErrorType) *Error {
msg.Type = flags
return msg
}
func (msg *Error) SetMeta(data interface{}) *Error {
msg.Meta = data
return msg
}
func (msg *Error) JSON() interface{} {
json := H{}
if msg.Meta != nil {
value := reflect.ValueOf(msg.Meta)
switch value.Kind() {
case reflect.Struct:
return msg.Meta
case reflect.Map:
for _, key := range value.MapKeys() {
json[key.String()] = value.MapIndex(key).Interface()
}
default:
json["meta"] = msg.Meta
}
}
if _, ok := json["error"]; !ok {
json["error"] = msg.Error()
}
return json
}
2018-11-16 03:10:19 +00:00
// MarshalJSON implements the json.Marshaller interface.
2018-08-14 09:25:34 +00:00
func (msg *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(msg.JSON())
}
2018-11-16 03:10:19 +00:00
// Error implements the error interface
2018-08-14 09:25:34 +00:00
func (msg Error) Error() string {
return msg.Err.Error()
}
func (msg *Error) IsType(flags ErrorType) bool {
return (msg.Type & flags) > 0
}
2018-11-16 03:10:19 +00:00
// ByType returns a readonly copy filtered the byte.
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
2018-08-14 09:25:34 +00:00
func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
if len(a) == 0 {
return nil
}
if typ == ErrorTypeAny {
return a
}
var result errorMsgs
for _, msg := range a {
if msg.IsType(typ) {
result = append(result, msg)
}
}
return result
}
2018-11-16 03:10:19 +00:00
// Last returns the last error in the slice. It returns nil if the array is empty.
// Shortcut for errors[len(errors)-1].
2018-08-14 09:25:34 +00:00
func (a errorMsgs) Last() *Error {
2018-11-16 03:10:19 +00:00
if length := len(a); length > 0 {
2018-08-14 09:25:34 +00:00
return a[length-1]
}
return nil
}
2018-11-16 03:10:19 +00:00
// Errors returns an array will all the error messages.
2018-08-14 09:25:34 +00:00
// Example:
// c.Error(errors.New("first"))
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
func (a errorMsgs) Errors() []string {
if len(a) == 0 {
return nil
}
errorStrings := make([]string, len(a))
for i, err := range a {
errorStrings[i] = err.Error()
}
return errorStrings
}
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
}
}
func (a errorMsgs) MarshalJSON() ([]byte, error) {
return json.Marshal(a.JSON())
}
func (a errorMsgs) String() string {
if len(a) == 0 {
return ""
}
var buffer bytes.Buffer
for i, msg := range a {
2018-11-16 03:10:19 +00:00
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
2018-08-14 09:25:34 +00:00
if msg.Meta != nil {
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
}
}
return buffer.String()
}