1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/errors/errors_test.go

51 lines
1.0 KiB
Go
Raw Normal View History

2021-02-17 11:14:47 +02:00
package errors
import (
"errors"
"fmt"
"testing"
)
func TestError(t *testing.T) {
var (
base *Error
)
err := New(400, "domain", "reason", "message")
err2 := New(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)
2021-02-17 11:14:47 +02:00
}
if !errors.Is(werr, err) {
t.Errorf("should be equal: %v", err)
2021-02-17 11:14:47 +02:00
}
if !errors.Is(werr, err2) {
t.Errorf("should be equal: %v", err)
2021-02-17 11:14:47 +02:00
}
if !errors.As(err, &base) {
t.Errorf("should be matchs: %v", err)
2021-02-17 11:14:47 +02:00
}
if !IsBadRequest(err) {
t.Errorf("should be matchs: %v", err)
2021-02-17 11:14:47 +02:00
}
if code := Code(err); code != err2.Code {
t.Errorf("got %d want: %s", code, 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)
}
2021-02-17 11:14:47 +02:00
if err3.Metadata["foo"] != "bar" {
t.Error("not expected metadata")
2021-02-17 11:14:47 +02:00
}
}