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"
|
2015-05-22 02:43:43 +00:00
|
|
|
"reflect"
|
2017-07-08 08:49:09 +00:00
|
|
|
|
2018-08-30 11:04:03 +00:00
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
2015-03-26 13:10:46 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
|
2015-05-22 23:59:36 +00:00
|
|
|
type ErrorType uint64
|
|
|
|
|
2015-03-26 13:10:46 +00:00
|
|
|
const (
|
2018-09-15 07:21:54 +00:00
|
|
|
// ErrorTypeBind is used when Context.Bind() fails.
|
|
|
|
ErrorTypeBind ErrorType = 1 << 63
|
|
|
|
// ErrorTypeRender is used when Context.Render() fails.
|
|
|
|
ErrorTypeRender ErrorType = 1 << 62
|
|
|
|
// ErrorTypePrivate indicates a private error.
|
2015-05-22 23:59:36 +00:00
|
|
|
ErrorTypePrivate ErrorType = 1 << 0
|
2018-09-15 07:21:54 +00:00
|
|
|
// ErrorTypePublic indicates a public error.
|
|
|
|
ErrorTypePublic ErrorType = 1 << 1
|
|
|
|
// ErrorTypeAny indicates other any error.
|
2015-05-22 23:59:36 +00:00
|
|
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
|
|
|
ErrorTypeNu = 2
|
2015-03-26 13:10:46 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// Error represents a error's specification.
|
2017-07-05 07:47:36 +00:00
|
|
|
type Error struct {
|
|
|
|
Err error
|
|
|
|
Type ErrorType
|
|
|
|
Meta interface{}
|
|
|
|
}
|
2015-05-22 17:21:35 +00:00
|
|
|
|
2017-07-05 07:47:36 +00:00
|
|
|
type errorMsgs []*Error
|
2015-05-22 01:25:21 +00:00
|
|
|
|
2015-05-22 14:39:15 +00:00
|
|
|
var _ error = &Error{}
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// SetType sets the error's type.
|
2015-05-22 23:59:36 +00:00
|
|
|
func (msg *Error) SetType(flags ErrorType) *Error {
|
2015-05-22 14:39:15 +00:00
|
|
|
msg.Type = flags
|
2015-05-22 01:25:21 +00:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// SetMeta sets the error's meta data.
|
2015-05-22 14:39:15 +00:00
|
|
|
func (msg *Error) SetMeta(data interface{}) *Error {
|
|
|
|
msg.Meta = data
|
2015-05-22 01:25:21 +00:00
|
|
|
return msg
|
2015-03-26 13:10:46 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:39:15 +00:00
|
|
|
func (msg *Error) JSON() interface{} {
|
2015-05-22 02:43:43 +00:00
|
|
|
json := H{}
|
2015-05-22 14:39:15 +00:00
|
|
|
if msg.Meta != nil {
|
|
|
|
value := reflect.ValueOf(msg.Meta)
|
2015-05-22 02:43:43 +00:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Struct:
|
2015-05-22 14:39:15 +00:00
|
|
|
return msg.Meta
|
2015-05-22 02:43:43 +00:00
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range value.MapKeys() {
|
|
|
|
json[key.String()] = value.MapIndex(key).Interface()
|
|
|
|
}
|
2015-05-22 14:39:15 +00:00
|
|
|
default:
|
|
|
|
json["meta"] = msg.Meta
|
2015-05-22 02:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := json["error"]; !ok {
|
|
|
|
json["error"] = msg.Error()
|
|
|
|
}
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2017-08-16 03:55:50 +00:00
|
|
|
// MarshalJSON implements the json.Marshaller interface.
|
2015-05-22 16:34:42 +00:00
|
|
|
func (msg *Error) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(msg.JSON())
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// Error implements the error interface.
|
2016-05-29 20:08:24 +00:00
|
|
|
func (msg Error) Error() string {
|
2015-05-22 01:43:39 +00:00
|
|
|
return msg.Err.Error()
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// IsType judges one error.
|
2015-06-12 19:33:16 +00:00
|
|
|
func (msg *Error) IsType(flags ErrorType) bool {
|
|
|
|
return (msg.Type & flags) > 0
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:28:16 +00:00
|
|
|
// ByType returns a readonly copy filtered the byte.
|
2017-08-16 03:55:50 +00:00
|
|
|
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
|
2015-05-22 23:59:36 +00:00
|
|
|
func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
|
2015-03-26 13:10:46 +00:00
|
|
|
if len(a) == 0 {
|
2015-06-06 15:24:16 +00:00
|
|
|
return nil
|
2015-03-26 13:10:46 +00:00
|
|
|
}
|
2015-06-12 19:34:06 +00:00
|
|
|
if typ == ErrorTypeAny {
|
|
|
|
return a
|
|
|
|
}
|
2016-04-14 23:16:46 +00:00
|
|
|
var result errorMsgs
|
2015-03-26 13:10:46 +00:00
|
|
|
for _, msg := range a {
|
2015-06-12 19:34:06 +00:00
|
|
|
if msg.IsType(typ) {
|
2015-03-26 13:10:46 +00:00
|
|
|
result = append(result, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:28:16 +00:00
|
|
|
// Last returns the last error in the slice. It returns nil if the array is empty.
|
2017-08-16 03:55:50 +00:00
|
|
|
// Shortcut for errors[len(errors)-1].
|
2015-05-22 14:39:15 +00:00
|
|
|
func (a errorMsgs) Last() *Error {
|
2017-07-06 01:28:16 +00:00
|
|
|
if length := len(a); length > 0 {
|
2015-07-02 16:42:33 +00:00
|
|
|
return a[length-1]
|
2015-05-22 01:43:39 +00:00
|
|
|
}
|
2015-07-02 16:42:33 +00:00
|
|
|
return nil
|
2015-05-22 01:43:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 01:28:16 +00:00
|
|
|
// Errors returns an array will all the error messages.
|
2016-01-26 21:40:29 +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"}
|
2015-05-09 01:34:43 +00:00
|
|
|
func (a errorMsgs) Errors() []string {
|
|
|
|
if len(a) == 0 {
|
2015-05-30 12:45:13 +00:00
|
|
|
return nil
|
2015-05-09 01:34:43 +00:00
|
|
|
}
|
2015-05-12 13:22:13 +00:00
|
|
|
errorStrings := make([]string, len(a))
|
2015-05-09 01:34:43 +00:00
|
|
|
for i, err := range a {
|
2015-05-22 01:43:39 +00:00
|
|
|
errorStrings[i] = err.Error()
|
2015-05-09 01:34:43 +00:00
|
|
|
}
|
2015-05-12 13:22:13 +00:00
|
|
|
return errorStrings
|
2015-05-09 01:34:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 02:43:43 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:21:54 +00:00
|
|
|
// MarshalJSON implements the json.Marshaller interface.
|
2015-05-22 16:34:42 +00:00
|
|
|
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(a.JSON())
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-29 02:50:14 +00:00
|
|
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
|
2015-05-22 14:39:15 +00:00
|
|
|
if msg.Meta != nil {
|
|
|
|
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
|
|
|
}
|
2015-03-26 13:10:46 +00:00
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
}
|