|
| 1 | +package errors_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + devboxerr "github.com/junixlabs/devbox/internal/errors" |
| 9 | +) |
| 10 | + |
| 11 | +func TestConfigError(t *testing.T) { |
| 12 | + inner := fmt.Errorf("file not found") |
| 13 | + err := devboxerr.NewConfigError("invalid config", "Run devbox init", inner) |
| 14 | + |
| 15 | + if err.Error() != "invalid config: file not found" { |
| 16 | + t.Errorf("unexpected message: %s", err.Error()) |
| 17 | + } |
| 18 | + |
| 19 | + if !errors.Is(err, inner) { |
| 20 | + t.Error("errors.Is should match inner error") |
| 21 | + } |
| 22 | + |
| 23 | + var ce *devboxerr.ConfigError |
| 24 | + if !errors.As(err, &ce) { |
| 25 | + t.Error("errors.As should match ConfigError") |
| 26 | + } |
| 27 | + |
| 28 | + if ce.GetSuggestion() != "Run devbox init" { |
| 29 | + t.Errorf("unexpected suggestion: %s", ce.GetSuggestion()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestConfigErrorNoInner(t *testing.T) { |
| 34 | + err := devboxerr.NewConfigError("missing name", "Add 'name' to devbox.yaml", nil) |
| 35 | + |
| 36 | + if err.Error() != "missing name" { |
| 37 | + t.Errorf("unexpected message: %s", err.Error()) |
| 38 | + } |
| 39 | + |
| 40 | + if err.Unwrap() != nil { |
| 41 | + t.Error("Unwrap should return nil") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestConnectionError(t *testing.T) { |
| 46 | + inner := fmt.Errorf("connection refused") |
| 47 | + err := devboxerr.NewConnectionError("ssh failed", "Check SSH connectivity", inner) |
| 48 | + |
| 49 | + if err.Error() != "ssh failed: connection refused" { |
| 50 | + t.Errorf("unexpected message: %s", err.Error()) |
| 51 | + } |
| 52 | + |
| 53 | + var ce *devboxerr.ConnectionError |
| 54 | + if !errors.As(err, &ce) { |
| 55 | + t.Error("errors.As should match ConnectionError") |
| 56 | + } |
| 57 | + |
| 58 | + if ce.GetSuggestion() != "Check SSH connectivity" { |
| 59 | + t.Errorf("unexpected suggestion: %s", ce.GetSuggestion()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestDockerError(t *testing.T) { |
| 64 | + err := devboxerr.NewDockerError("container failed", "Run: docker ps", nil) |
| 65 | + |
| 66 | + if err.Error() != "container failed" { |
| 67 | + t.Errorf("unexpected message: %s", err.Error()) |
| 68 | + } |
| 69 | + |
| 70 | + var de *devboxerr.DockerError |
| 71 | + if !errors.As(err, &de) { |
| 72 | + t.Error("errors.As should match DockerError") |
| 73 | + } |
| 74 | + |
| 75 | + if de.GetSuggestion() != "Run: docker ps" { |
| 76 | + t.Errorf("unexpected suggestion: %s", de.GetSuggestion()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestSuggestibleInterface(t *testing.T) { |
| 81 | + cases := []struct { |
| 82 | + name string |
| 83 | + err devboxerr.Suggestible |
| 84 | + want string |
| 85 | + }{ |
| 86 | + {"ConfigError", devboxerr.NewConfigError("x", "fix config", nil), "fix config"}, |
| 87 | + {"ConnectionError", devboxerr.NewConnectionError("x", "fix conn", nil), "fix conn"}, |
| 88 | + {"DockerError", devboxerr.NewDockerError("x", "fix docker", nil), "fix docker"}, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tc := range cases { |
| 92 | + t.Run(tc.name, func(t *testing.T) { |
| 93 | + if tc.err.GetSuggestion() != tc.want { |
| 94 | + t.Errorf("got %q, want %q", tc.err.GetSuggestion(), tc.want) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestUnwrapChain(t *testing.T) { |
| 101 | + root := fmt.Errorf("root cause") |
| 102 | + wrapped := fmt.Errorf("middle: %w", root) |
| 103 | + err := devboxerr.NewConnectionError("top level", "suggestion", wrapped) |
| 104 | + |
| 105 | + if !errors.Is(err, root) { |
| 106 | + t.Error("errors.Is should traverse the full chain to root") |
| 107 | + } |
| 108 | +} |
0 commit comments