You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/errors/errors_test.go

47 lines
949 B

package errors
import (
"errors"
"fmt"
"testing"
)
func TestError(t *testing.T) {
var (
base *Error
)
err := Errorf(400, "domain", "reason", "message")
err2 := Errorf(400, "domain", "reason", "message")
err3 := err.WithMetadata(map[string]string{
"foo": "bar",
})
werr := fmt.Errorf("wrap %w", err)
if errors.Is(err, new(Error)) {
t.Errorf("should not be equal: %v", err)
}
if !errors.Is(werr, err) {
t.Errorf("should be equal: %v", err)
}
if !errors.Is(werr, err2) {
t.Errorf("should be equal: %v", err)
}
if !errors.As(err, &base) {
t.Errorf("should be matchs: %v", err)
}
if !IsBadRequest(err) {
t.Errorf("should be matchs: %v", err)
}
if domain := Domain(err); domain != err2.Domain {
t.Errorf("got %s want: %s", domain, err)
}
if reason := Reason(err); reason != err3.Reason {
t.Errorf("got %s want: %s", reason, err)
}
if err3.Metadata["foo"] != "bar" {
t.Error("not expected metadata")
}
}