2017-02-21 17:14:07 -05:00
|
|
|
package errors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-02-10 09:02:28 -05:00
|
|
|
"strings"
|
2017-02-21 17:14:07 -05:00
|
|
|
"testing"
|
|
|
|
|
2018-11-15 05:17:20 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/log"
|
2017-02-21 17:14:07 -05:00
|
|
|
)
|
|
|
|
|
2017-04-06 15:13:17 -04:00
|
|
|
func TestError(t *testing.T) {
|
2017-02-21 17:14:07 -05:00
|
|
|
err := New("TestError")
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError2").Base(io.EOF)
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError3").Base(io.EOF).AtWarning()
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError4").Base(io.EOF).AtWarning()
|
|
|
|
err = New("TestError5").Base(err)
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
if v := err.Error(); !strings.Contains(v, "EOF") {
|
|
|
|
t.Error("error: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
2017-04-06 15:13:17 -04:00
|
|
|
|
2018-09-30 12:39:53 -04:00
|
|
|
type e struct{}
|
|
|
|
|
2017-04-06 15:13:17 -04:00
|
|
|
func TestErrorMessage(t *testing.T) {
|
|
|
|
data := []struct {
|
|
|
|
err error
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2018-09-30 12:39:53 -04:00
|
|
|
err: New("a").Base(New("b")).WithPathObj(e{}),
|
2021-04-02 08:44:33 -04:00
|
|
|
msg: "common/errors_test: a > b",
|
2017-04-06 15:13:17 -04:00
|
|
|
},
|
|
|
|
{
|
2018-09-30 12:39:53 -04:00
|
|
|
err: New("a").Base(New("b").WithPathObj(e{})),
|
2021-04-02 08:44:33 -04:00
|
|
|
msg: "a > common/errors_test: b",
|
2017-04-06 15:13:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range data {
|
2018-11-15 05:17:20 -05:00
|
|
|
if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
|
|
|
|
t.Error(diff)
|
2018-07-13 08:36:09 -04:00
|
|
|
}
|
2017-04-06 15:13:17 -04:00
|
|
|
}
|
|
|
|
}
|