mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add gerror.ApiCurrent impements for gerror.Error
This commit is contained in:
@ -16,19 +16,25 @@ import (
|
||||
|
||||
// ApiStack is the interface for Stack feature.
|
||||
type ApiStack interface {
|
||||
Error() string // It should be en error.
|
||||
Error() string // It should be an error.
|
||||
Stack() string
|
||||
}
|
||||
|
||||
// ApiCause is the interface for Cause feature.
|
||||
type ApiCause interface {
|
||||
Error() string // It should be en error.
|
||||
Error() string // It should be an error.
|
||||
Cause() error
|
||||
}
|
||||
|
||||
// ApiLevel is the interface for Current/Next feature.
|
||||
type ApiLevel interface {
|
||||
// ApiCurrent is the interface for Current feature.
|
||||
type ApiCurrent interface {
|
||||
Error() string // It should be an error.
|
||||
Current() error
|
||||
}
|
||||
|
||||
// ApiNext is the interface for Next feature.
|
||||
type ApiNext interface {
|
||||
Error() string // It should be an error.
|
||||
Next() error
|
||||
}
|
||||
|
||||
@ -133,7 +139,7 @@ func Current(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if e, ok := err.(ApiLevel); ok {
|
||||
if e, ok := err.(ApiCurrent); ok {
|
||||
return e.Current()
|
||||
}
|
||||
return err
|
||||
@ -145,7 +151,7 @@ func Next(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if e, ok := err.(ApiLevel); ok {
|
||||
if e, ok := err.(ApiNext); ok {
|
||||
return e.Next()
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gvalid
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
"strings"
|
||||
)
|
||||
@ -131,6 +132,15 @@ func (e *Error) FirstString() (err string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Current is alis of FirstString, which implements interface gerror.ApiCurrent.
|
||||
func (e *Error) Current() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := e.FirstRule()
|
||||
return errors.New(err)
|
||||
}
|
||||
|
||||
// String returns all error messages as string, multiple error messages joined using char ';'.
|
||||
func (e *Error) String() string {
|
||||
if e == nil {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
@ -197,5 +198,7 @@ func Test_Sequence(t *testing.T) {
|
||||
r, s := err.FirstRule()
|
||||
t.Assert(r, "required")
|
||||
t.Assert(s, "账号不能为空")
|
||||
|
||||
t.Assert(gerror.Current(err), "账号不能为空")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user