mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve error handling for custom validation rule for package gvalid
This commit is contained in:
@ -21,7 +21,7 @@ import (
|
||||
type Error struct {
|
||||
error error // Wrapped error.
|
||||
stack stack // Stack array, which records the stack information when this error is created or wrapped.
|
||||
text string // Error text, which is created by New* functions.
|
||||
text string // Custom Error text when Error is created, might be empty when its code is not nil.
|
||||
code gcode.Code // Error code if necessary.
|
||||
}
|
||||
|
||||
@ -31,8 +31,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// goRootForFilter is used for stack filtering purpose.
|
||||
// Mainly for development environment.
|
||||
// goRootForFilter is used for stack filtering in development environment purpose.
|
||||
goRootForFilter = runtime.GOROOT()
|
||||
)
|
||||
|
||||
@ -107,18 +106,18 @@ func (err *Error) Format(s fmt.State, verb rune) {
|
||||
switch {
|
||||
case s.Flag('-'):
|
||||
if err.text != "" {
|
||||
io.WriteString(s, err.text)
|
||||
_, _ = io.WriteString(s, err.text)
|
||||
} else {
|
||||
io.WriteString(s, err.Error())
|
||||
_, _ = io.WriteString(s, err.Error())
|
||||
}
|
||||
case s.Flag('+'):
|
||||
if verb == 's' {
|
||||
io.WriteString(s, err.Stack())
|
||||
_, _ = io.WriteString(s, err.Stack())
|
||||
} else {
|
||||
io.WriteString(s, err.Error()+"\n"+err.Stack())
|
||||
_, _ = io.WriteString(s, err.Error()+"\n"+err.Stack())
|
||||
}
|
||||
default:
|
||||
io.WriteString(s, err.Error())
|
||||
_, _ = io.WriteString(s, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -176,6 +175,14 @@ func (err *Error) Next() error {
|
||||
return err.error
|
||||
}
|
||||
|
||||
// SetCode updates the internal code with given code.
|
||||
func (err *Error) SetCode(code gcode.Code) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
err.code = code
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// Note that do not use pointer as its receiver here.
|
||||
func (err *Error) MarshalJSON() ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user