| 1 | package usererror |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/x509" |
| 6 | "errors" |
| 7 | "io/fs" |
| 8 | "net" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "syscall" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func TestKnownFailureIsReadableAndPreservesCause(t *testing.T) { |
| 16 | for _, tc := range []struct { |
| 17 | cause error |
| 18 | reason string |
| 19 | }{ |
| 20 | {context.DeadlineExceeded, "超时"}, |
| 21 | {&url.Error{Op: "GET", URL: "https://user:[email protected]/secret", Err: &net.DNSError{Err: "no such host", Name: "secret.example"}}, "域名"}, |
| 22 | {&fs.PathError{Op: "open", Path: "/private-secret/file", Err: fs.ErrPermission}, "拒绝访问"}, |
| 23 | {syscall.ENOSPC, "空间不足"}, |
| 24 | {syscall.ECONNREFUSED, "拒绝了连接"}, |
| 25 | {x509.UnknownAuthorityError{}, "证书"}, |
| 26 | } { |
| 27 | err := Wrap("连接更新源", tc.cause, "请检查所选代理。") |
| 28 | if !strings.Contains(err.Error(), tc.reason) || !strings.Contains(err.Error(), "连接更新源") || strings.Contains(err.Error(), "private") { |
| 29 | t.Fatalf("unsafe or vague error: %v", err) |
| 30 | } |
| 31 | if !errors.Is(err, tc.cause) { |
| 32 | t.Fatal("original error identity lost") |
| 33 | } |
| 34 | } |
| 35 | precise := errors.New("上传窗口已关闭,请重新选择文件") |
| 36 | if Display("上传", precise, "") != precise { |
| 37 | t.Fatal("specific application message was replaced") |
| 38 | } |
| 39 | localized := &fs.PathError{Op: "open", Path: "/Users/测试用户/private-secret", Err: fs.ErrPermission} |
| 40 | if message := Display("保存配置", localized, "请检查写入权限。").Error(); !strings.Contains(message, "拒绝访问") || strings.Contains(message, "private-secret") { |
| 41 | t.Fatalf("localized system error bypassed safe explanation: %s", message) |
| 42 | } |
| 43 | } |