errors: add FromError func (#1362)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
cd04111e3d
commit
41f8a8cb00
@ -116,6 +116,7 @@ func InternalServerError(id, format string, a ...interface{}) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Equal tries to compare errors
|
||||||
func Equal(err1 error, err2 error) bool {
|
func Equal(err1 error, err2 error) bool {
|
||||||
verr1, ok1 := err1.(*Error)
|
verr1, ok1 := err1.(*Error)
|
||||||
verr2, ok2 := err2.(*Error)
|
verr2, ok2 := err2.(*Error)
|
||||||
@ -134,3 +135,12 @@ func Equal(err1 error, err2 error) bool {
|
|||||||
|
|
||||||
return true
|
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())
|
||||||
|
}
|
||||||
|
@ -6,6 +6,20 @@ import (
|
|||||||
"testing"
|
"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) {
|
func TestEqual(t *testing.T) {
|
||||||
err1 := NotFound("myid1", "msg1")
|
err1 := NotFound("myid1", "msg1")
|
||||||
err2 := NotFound("myid2", "msg2")
|
err2 := NotFound("myid2", "msg2")
|
||||||
|
Loading…
Reference in New Issue
Block a user