gin/errors.go

175 lines
3.9 KiB
Go
Raw Normal View History

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