diff --git a/erkfc.go b/erk_fc.go similarity index 89% rename from erkfc.go rename to erk_fc.go index ccc0c5d..2cc99b7 100644 --- a/erkfc.go +++ b/erk_fc.go @@ -33,3 +33,11 @@ func NewErkFm(erkFc func(format string, args ...interface{}) *errors.Error, msg func NewErkFmx(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { return NewErkFm(erkFc, msg, "erx") } + +func As(erx error) (erk *errors.Error, ok bool) { + return erk, errors.As(erx, &erk) +} + +func Is(erx error, target error) (ok bool) { + return errors.Is(erx, target) +} diff --git a/erk_fc_test.go b/erk_fc_test.go new file mode 100644 index 0000000..d6b267c --- /dev/null +++ b/erk_fc_test.go @@ -0,0 +1,59 @@ +package erkkratos + +import ( + "testing" + + "github.com/go-kratos/kratos/v2/errors" + "github.com/orzkratos/erkkratos/internal/errors_example" + "github.com/stretchr/testify/require" + "github.com/yyle88/erero" +) + +func TestNewErkFsc(t *testing.T) { + erk := NewErkFsc(errors_example.ErrorServerDbError, "erk")(erero.New("wac")) + require.NotNil(t, erk) + require.True(t, errors_example.IsServerDbError(erk)) + t.Log(erk) +} + +func TestNewErkFmx(t *testing.T) { + erk := NewErkFmx(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) + require.NotNil(t, erk) + require.True(t, errors_example.IsServerDbError(erk)) + t.Log(erk) +} + +func TestNewErkFmx2(t *testing.T) { + erk := NewErkFmx(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) + require.NotNil(t, erk) + require.True(t, errors_example.IsServerDbError(erk)) + t.Log(erk) + + var err error + err = erk + + var target *errors.Error + ok := errors.As(err, &target) + t.Log(ok) + t.Log(target) +} + +func TestAs(t *testing.T) { + { + var erk = errors_example.ErrorServerDbError("wrong") + var erx error = erk + res, ok := As(erx) + require.True(t, ok) + t.Log(res) + require.NotNil(t, res) + } + + { + var erk *errors.Error + var erx error = erk + res, ok := As(erx) + require.True(t, ok) + t.Log(res) + require.Nil(t, res) + } +} diff --git a/erkfc_test.go b/erkfc_test.go deleted file mode 100644 index 8329a3e..0000000 --- a/erkfc_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package erkkratos - -import ( - "testing" - - "github.com/orzkratos/erkkratos/internal/errors_example" - "github.com/stretchr/testify/require" - "github.com/yyle88/erero" -) - -func TestNewErkFsc(t *testing.T) { - erk := NewErkFsc(errors_example.ErrorServerDbError, "erk")(erero.New("wac")) - require.NotNil(t, erk) - require.True(t, errors_example.IsServerDbError(erk)) - t.Log(erk) -} - -func TestNewErkFmx(t *testing.T) { - erk := NewErkFmx(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) - require.NotNil(t, erk) - require.True(t, errors_example.IsServerDbError(erk)) - t.Log(erk) -} diff --git a/go.mod b/go.mod index f7fdd82..a02648f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.6 require ( github.com/go-kratos/kratos/v2 v2.8.0 github.com/stretchr/testify v1.9.0 - github.com/yyle88/erero v1.0.8 + github.com/yyle88/erero v1.0.9 ) require ( @@ -18,7 +18,7 @@ require ( go.uber.org/zap v1.27.0 // indirect golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.17.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c // indirect google.golang.org/grpc v1.65.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect diff --git a/go.sum b/go.sum index 7bb83a8..a114c90 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/yyle88/erero v1.0.8 h1:UVXC5qGJ+UD7f2xsj75eVScLzVhiCKg7eXfP0yKNSqM= -github.com/yyle88/erero v1.0.8/go.mod h1:+qbuev+H/83Hr/zVTjkea2BxxUxAZWNeDVkoFu7ckgk= +github.com/yyle88/erero v1.0.9 h1:E6Llc9IsuSR151CPcE8Y97BapD+IgDR3yMwo+6CRnp4= +github.com/yyle88/erero v1.0.9/go.mod h1:+qbuev+H/83Hr/zVTjkea2BxxUxAZWNeDVkoFu7ckgk= github.com/yyle88/mutexmap v1.0.3 h1:s5OqqR/oDW5ABhKZi25DLd4IE/KPPwlp6UyzqZayfXo= github.com/yyle88/mutexmap v1.0.3/go.mod h1:xUqVmNE2/y4AMz5a8O3a4ik9Ua5xskEaLQ+ICnAAl8E= github.com/yyle88/zaplog v0.0.7 h1:Kqwk+q4QatgS5Gyor2xGwSEQZ3U8Hai/ojkAz9BzOMY= @@ -34,8 +34,8 @@ golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c h1:Kqjm4WpoWvwhMPcrAczoTyMySQmYa9Wy2iL6Con4zn8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=