2020-11-25 11:01:53 +00:00
|
|
|
package errors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
. "github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/common/log"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
err := New("TestError")
|
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = New("TestError2").Base(io.EOF)
|
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = New("TestError3").Base(io.EOF).AtWarning()
|
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = New("TestError4").Base(io.EOF).AtWarning()
|
|
|
|
err = New("TestError5").Base(err)
|
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
if v := err.Error(); !strings.Contains(v, "EOF") {
|
|
|
|
t.Error("error: ", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type e struct{}
|
|
|
|
|
|
|
|
func TestErrorMessage(t *testing.T) {
|
|
|
|
data := []struct {
|
|
|
|
err error
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
err: New("a").Base(New("b")).WithPathObj(e{}),
|
2020-12-04 01:36:16 +00:00
|
|
|
msg: "common/errors_test: a > b",
|
2020-11-25 11:01:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
err: New("a").Base(New("b").WithPathObj(e{})),
|
2020-12-04 01:36:16 +00:00
|
|
|
msg: "a > common/errors_test: b",
|
2020-11-25 11:01:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range data {
|
|
|
|
if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|