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

52 lines
1000 B
Go
Raw Normal View History

2021-02-17 11:14:47 +02:00
package errors
import (
"errors"
"fmt"
"net/http"
2021-02-17 11:14:47 +02:00
"testing"
)
func TestError(t *testing.T) {
var (
base *Error
)
err := Newf(http.StatusBadRequest, "reason", "message")
err2 := Newf(http.StatusBadRequest, "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 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
}
gs := err.GRPCStatus()
se := FromError(gs.Err())
if se.Reason != se.Reason {
t.Errorf("got %+v want %+v", se, err)
}
2021-02-17 11:14:47 +02:00
}