|
@@ -12,18 +12,24 @@ import (
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
|
"github.com/gin-gonic/gin/internal/json"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
|
|
|
type ErrorType uint64
|
|
type ErrorType uint64
|
|
|
|
|
|
|
|
const (
|
|
const (
|
|
|
- ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
|
|
|
|
|
- ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
|
|
|
|
|
|
|
+ // 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
|
|
ErrorTypePrivate ErrorType = 1 << 0
|
|
|
- ErrorTypePublic ErrorType = 1 << 1
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // ErrorTypePublic indicates a public error.
|
|
|
|
|
+ ErrorTypePublic ErrorType = 1 << 1
|
|
|
|
|
+ // ErrorTypeAny indicates other any error.
|
|
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
|
|
ErrorTypeNu = 2
|
|
ErrorTypeNu = 2
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// Error represents a error's specification.
|
|
|
type Error struct {
|
|
type Error struct {
|
|
|
Err error
|
|
Err error
|
|
|
Type ErrorType
|
|
Type ErrorType
|
|
@@ -34,11 +40,13 @@ type errorMsgs []*Error
|
|
|
|
|
|
|
|
var _ error = &Error{}
|
|
var _ error = &Error{}
|
|
|
|
|
|
|
|
|
|
+// SetType sets the error's type.
|
|
|
func (msg *Error) SetType(flags ErrorType) *Error {
|
|
func (msg *Error) SetType(flags ErrorType) *Error {
|
|
|
msg.Type = flags
|
|
msg.Type = flags
|
|
|
return msg
|
|
return msg
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// SetMeta sets the error's meta data.
|
|
|
func (msg *Error) SetMeta(data interface{}) *Error {
|
|
func (msg *Error) SetMeta(data interface{}) *Error {
|
|
|
msg.Meta = data
|
|
msg.Meta = data
|
|
|
return msg
|
|
return msg
|
|
@@ -70,11 +78,12 @@ func (msg *Error) MarshalJSON() ([]byte, error) {
|
|
|
return json.Marshal(msg.JSON())
|
|
return json.Marshal(msg.JSON())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Error implements the error interface
|
|
|
|
|
|
|
+// Error implements the error interface.
|
|
|
func (msg Error) Error() string {
|
|
func (msg Error) Error() string {
|
|
|
return msg.Err.Error()
|
|
return msg.Err.Error()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// IsType judges one error.
|
|
|
func (msg *Error) IsType(flags ErrorType) bool {
|
|
func (msg *Error) IsType(flags ErrorType) bool {
|
|
|
return (msg.Type & flags) > 0
|
|
return (msg.Type & flags) > 0
|
|
|
}
|
|
}
|
|
@@ -138,6 +147,7 @@ func (a errorMsgs) JSON() interface{} {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// MarshalJSON implements the json.Marshaller interface.
|
|
|
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
|
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
|
|
return json.Marshal(a.JSON())
|
|
return json.Marshal(a.JSON())
|
|
|
}
|
|
}
|