1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

errors: add FromError func (#1362)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov 2020-03-18 03:10:38 +03:00 committed by GitHub
parent cd04111e3d
commit 41f8a8cb00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -116,6 +116,7 @@ func InternalServerError(id, format string, a ...interface{}) error {
}
}
// Equal tries to compare errors
func Equal(err1 error, err2 error) bool {
verr1, ok1 := err1.(*Error)
verr2, ok2 := err2.(*Error)
@ -134,3 +135,12 @@ func Equal(err1 error, err2 error) bool {
return true
}
// FromError try to convert go error to *Error
func FromError(err error) *Error {
if verr, ok := err.(*Error); ok && verr != nil {
return verr
}
return Parse(err.Error())
}

View File

@ -6,6 +6,20 @@ import (
"testing"
)
func TestFromError(t *testing.T) {
err := NotFound("go.micro.test", "%s", "example")
merr := FromError(err)
if merr.Id != "go.micro.test" || merr.Code != 404 {
t.Fatalf("invalid conversation %v != %v", err, merr)
}
err = er.New(err.Error())
merr = FromError(err)
if merr.Id != "go.micro.test" || merr.Code != 404 {
t.Fatalf("invalid conversation %v != %v", err, merr)
}
}
func TestEqual(t *testing.T) {
err1 := NotFound("myid1", "msg1")
err2 := NotFound("myid2", "msg2")