2022-09-28 19:30:40 +02:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2021-08-12 21:03:24 +02:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
|
|
|
|
type ErrorType uint64
|
|
|
|
|
|
|
|
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.
|
|
|
|
ErrorTypePrivate ErrorType = 1 << 0
|
|
|
|
// ErrorTypePublic indicates a public error.
|
|
|
|
ErrorTypePublic ErrorType = 1 << 1
|
|
|
|
// ErrorTypeAny indicates any other error.
|
|
|
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
|
|
|
// ErrorTypeNu indicates any other error.
|
|
|
|
ErrorTypeNu = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error represents a error's specification.
|
|
|
|
type Error struct {
|
|
|
|
Err error
|
|
|
|
Type ErrorType
|
2022-09-28 19:30:40 +02:00
|
|
|
Meta any
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type errorMsgs []*Error
|
|
|
|
|
2023-02-25 13:12:40 +01:00
|
|
|
var _ error = (*Error)(nil)
|
2021-08-12 21:03:24 +02:00
|
|
|
|
|
|
|
// SetType sets the error's type.
|
|
|
|
func (msg *Error) SetType(flags ErrorType) *Error {
|
|
|
|
msg.Type = flags
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMeta sets the error's meta data.
|
2022-09-28 19:30:40 +02:00
|
|
|
func (msg *Error) SetMeta(data any) *Error {
|
2021-08-12 21:03:24 +02:00
|
|
|
msg.Meta = data
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON creates a properly formatted JSON
|
2022-09-28 19:30:40 +02:00
|
|
|
func (msg *Error) JSON() any {
|
2021-08-12 21:03:24 +02:00
|
|
|
jsonData := 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() {
|
|
|
|
jsonData[key.String()] = value.MapIndex(key).Interface()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
jsonData["meta"] = msg.Meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := jsonData["error"]; !ok {
|
|
|
|
jsonData["error"] = msg.Error()
|
|
|
|
}
|
|
|
|
return jsonData
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return msg.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsType judges one error.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByType returns a readonly copy filtered the byte.
|
|
|
|
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last returns the last error in the slice. It returns nil if the array is empty.
|
|
|
|
// Shortcut for errors[len(errors)-1].
|
|
|
|
func (a errorMsgs) Last() *Error {
|
|
|
|
if length := len(a); length > 0 {
|
|
|
|
return a[length-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:30:40 +02:00
|
|
|
// Errors returns an array with all the error messages.
|
2021-08-12 21:03:24 +02:00
|
|
|
// Example:
|
2023-02-25 13:12:40 +01:00
|
|
|
//
|
|
|
|
// c.Error(errors.New("first"))
|
|
|
|
// c.Error(errors.New("second"))
|
|
|
|
// c.Error(errors.New("third"))
|
|
|
|
// c.Errors.Errors() // == []string{"first", "second", "third"}
|
2021-08-12 21:03:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:30:40 +02:00
|
|
|
func (a errorMsgs) JSON() any {
|
2021-08-12 21:03:24 +02:00
|
|
|
switch length := len(a); length {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return a.Last().JSON()
|
|
|
|
default:
|
2022-09-28 19:30:40 +02:00
|
|
|
jsonData := make([]any, length)
|
2021-08-12 21:03:24 +02:00
|
|
|
for i, err := range a {
|
|
|
|
jsonData[i] = err.JSON()
|
|
|
|
}
|
|
|
|
return jsonData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaller interface.
|
|
|
|
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(a.JSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a errorMsgs) String() string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buffer strings.Builder
|
|
|
|
for i, msg := range a {
|
|
|
|
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
|
|
|
|
if msg.Meta != nil {
|
|
|
|
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
}
|