improve gerror

This commit is contained in:
John
2019-07-13 14:24:44 +08:00
parent 28e5c33e81
commit edd93c39a3
4 changed files with 40 additions and 8 deletions

View File

@ -9,9 +9,18 @@ package gerror
import (
"fmt"
"github.com/gogf/gf/g/util/gconv"
)
type ApiStack interface {
Stack() string
}
type ApiCause interface {
Cause() error
}
// New returns an error that formats as the given value.
func New(value interface{}) error {
if value == nil {
@ -61,7 +70,7 @@ func Wrapf(err error, format string, args ...interface{}) error {
// Cause returns the root cause error.
func Cause(err error) error {
if err != nil {
if e, ok := err.(*Error); ok {
if e, ok := err.(ApiCause); ok {
return e.Cause()
}
}
@ -74,7 +83,7 @@ func Stack(err error) string {
if err == nil {
return ""
}
if e, ok := err.(*Error); !ok {
if e, ok := err.(ApiStack); ok {
return e.Stack()
}
return ""

View File

@ -237,7 +237,7 @@ func Test_CheckStruct_With_Inherit(t *testing.T) {
Pass Pass
}
user := &User{
Name: "john",
Name: "",
Pass: Pass{
Pass1: "1",
Pass2: "2",

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/g/os/glog"
"fmt"
"github.com/gogf/gf/g/errors/gerror"
)
@ -19,8 +19,8 @@ func ReadConfig() error {
}
func main() {
err := ReadConfig()
glog.Printf("%s\n%+s", err, err)
glog.Printf("%+v", err)
glog.Error(err)
//err := ReadConfig()
//glog.Printf("%s\n%+s", err, err)
//glog.Printf("%+v", err)
fmt.Printf("%+v", ReadConfig())
}

View File

@ -0,0 +1,23 @@
package main
import (
"errors"
"fmt"
"github.com/gogf/gf/g/errors/gerror"
)
func Error1() error {
return errors.New("test")
}
func Error2() error {
return gerror.New("test")
}
func main() {
err1 := Error1()
err2 := Error2()
fmt.Println("err1:\n", gerror.Stack(err1))
fmt.Println("err2:\n", gerror.Stack(err2))
}