errors_test.go 896 B

12345678910111213141516171819202122232425262728293031
  1. package kit
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "go.mongodb.org/mongo-driver/mongo"
  9. )
  10. func TestUpstreamHidesRawError(t *testing.T) {
  11. cases := []struct {
  12. err error
  13. kind string
  14. }{
  15. {errors.New(`pq: relation "site_info" does not exist (host=10.0.0.5:5432)`), "数据源返回错误"},
  16. {fmt.Errorf("query: %w", context.DeadlineExceeded), "请求超时"},
  17. {fmt.Errorf("find: %w", mongo.ErrNoDocuments), "没有找到对应记录"},
  18. }
  19. for _, c := range cases {
  20. text := UpstreamText("测试数据源", c.err)
  21. if strings.Contains(text, "10.0.0.5") || strings.Contains(text, "relation") || strings.Contains(text, "mongo") {
  22. t.Errorf("错误说明泄露了原始错误:%s", text)
  23. }
  24. if !strings.Contains(text, c.kind) || !strings.Contains(text, "错误编号") {
  25. t.Errorf("错误说明应包含类别 %s 与错误编号:%s", c.kind, text)
  26. }
  27. }
  28. }