|
| 1 | +package ui |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/fs" |
| 9 | + "net" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/safedep/pmg/usefulerror" |
| 15 | +) |
| 16 | + |
| 17 | +// errorMatcher defines how to detect and convert a specific error type |
| 18 | +type errorMatcher struct { |
| 19 | + match func(err error) bool |
| 20 | + convert func(err error) usefulerror.UsefulError |
| 21 | +} |
| 22 | + |
| 23 | +// errorMatchers is an ordered list of error matchers |
| 24 | +// Order matters - more specific matchers should come first |
| 25 | +var errorMatchers = []errorMatcher{ |
| 26 | + // File not found errors |
| 27 | + { |
| 28 | + match: func(err error) bool { |
| 29 | + return errors.Is(err, os.ErrNotExist) || errors.Is(err, fs.ErrNotExist) |
| 30 | + }, |
| 31 | + convert: func(err error) usefulerror.UsefulError { |
| 32 | + path := extractPathFromError(err) |
| 33 | + humanError := "File or directory not found" |
| 34 | + if path != "" { |
| 35 | + humanError = fmt.Sprintf("File or directory not found: %s", path) |
| 36 | + } |
| 37 | + |
| 38 | + return usefulerror.Useful(). |
| 39 | + WithCode(usefulerror.ErrCodeNotFound). |
| 40 | + WithHumanError(humanError). |
| 41 | + WithHelp("Check if the path exists"). |
| 42 | + WithAdditionalHelp("Use 'ls' to check directory contents"). |
| 43 | + Wrap(err) |
| 44 | + }, |
| 45 | + }, |
| 46 | + // Permission denied errors |
| 47 | + { |
| 48 | + match: func(err error) bool { |
| 49 | + return errors.Is(err, os.ErrPermission) || errors.Is(err, fs.ErrPermission) |
| 50 | + }, |
| 51 | + convert: func(err error) usefulerror.UsefulError { |
| 52 | + path := extractPathFromError(err) |
| 53 | + humanError := "Permission denied" |
| 54 | + if path != "" { |
| 55 | + humanError = fmt.Sprintf("Permission denied: %s", path) |
| 56 | + } |
| 57 | + return usefulerror.Useful(). |
| 58 | + WithCode(usefulerror.ErrCodePermissionDenied). |
| 59 | + WithHumanError(humanError). |
| 60 | + WithHelp("Check permissions or use sudo"). |
| 61 | + WithAdditionalHelp("Use 'ls -la' to check permissions"). |
| 62 | + Wrap(err) |
| 63 | + }, |
| 64 | + }, |
| 65 | + // Process exit errors |
| 66 | + { |
| 67 | + match: func(err error) bool { |
| 68 | + var exitErr *exec.ExitError |
| 69 | + return errors.As(err, &exitErr) |
| 70 | + }, |
| 71 | + convert: func(err error) usefulerror.UsefulError { |
| 72 | + var exitErr *exec.ExitError |
| 73 | + errors.As(err, &exitErr) |
| 74 | + exitCode := exitErr.ExitCode() |
| 75 | + return usefulerror.Useful(). |
| 76 | + WithCode(usefulerror.ErrCodeLifecycle). |
| 77 | + WithHumanError(fmt.Sprintf("Command failed with exit code %d", exitCode)). |
| 78 | + WithHelp("Check command output above"). |
| 79 | + WithAdditionalHelp("Run with PMG_DEBUG=true for more details"). |
| 80 | + Wrap(err) |
| 81 | + }, |
| 82 | + }, |
| 83 | + // Timeout errors (check before network errors since network timeouts also match) |
| 84 | + { |
| 85 | + match: func(err error) bool { |
| 86 | + return errors.Is(err, context.DeadlineExceeded) |
| 87 | + }, |
| 88 | + convert: func(err error) usefulerror.UsefulError { |
| 89 | + return usefulerror.Useful(). |
| 90 | + WithCode(usefulerror.ErrCodeTimeout). |
| 91 | + WithHumanError("Operation timed out"). |
| 92 | + WithHelp("Try again or check your network"). |
| 93 | + WithAdditionalHelp("Consider increasing timeout or retry later"). |
| 94 | + Wrap(err) |
| 95 | + }, |
| 96 | + }, |
| 97 | + // Canceled errors |
| 98 | + { |
| 99 | + match: func(err error) bool { |
| 100 | + return errors.Is(err, context.Canceled) |
| 101 | + }, |
| 102 | + convert: func(err error) usefulerror.UsefulError { |
| 103 | + return usefulerror.Useful(). |
| 104 | + WithCode(usefulerror.ErrCodeCanceled). |
| 105 | + WithHumanError("Operation was canceled"). |
| 106 | + Wrap(err) |
| 107 | + }, |
| 108 | + }, |
| 109 | + // Network errors |
| 110 | + { |
| 111 | + match: func(err error) bool { |
| 112 | + var netErr net.Error |
| 113 | + if errors.As(err, &netErr) { |
| 114 | + return true |
| 115 | + } |
| 116 | + // Also check for common network-related error messages |
| 117 | + errStr := err.Error() |
| 118 | + return strings.Contains(errStr, "connection refused") || |
| 119 | + strings.Contains(errStr, "no such host") || |
| 120 | + strings.Contains(errStr, "network is unreachable") |
| 121 | + }, |
| 122 | + convert: func(err error) usefulerror.UsefulError { |
| 123 | + var netErr net.Error |
| 124 | + if errors.As(err, &netErr) && netErr.Timeout() { |
| 125 | + return usefulerror.Useful(). |
| 126 | + WithCode(usefulerror.ErrCodeTimeout). |
| 127 | + WithHumanError("Network request timed out"). |
| 128 | + WithHelp("Check your internet connection"). |
| 129 | + WithAdditionalHelp("Consider increasing timeout or retry later"). |
| 130 | + Wrap(err) |
| 131 | + } |
| 132 | + return usefulerror.Useful(). |
| 133 | + WithCode(usefulerror.ErrCodeNetwork). |
| 134 | + WithHumanError("Network error occurred"). |
| 135 | + WithHelp("Check your internet connection"). |
| 136 | + WithAdditionalHelp("The package registry may be temporarily unavailable"). |
| 137 | + Wrap(err) |
| 138 | + }, |
| 139 | + }, |
| 140 | + // Unexpected EOF errors |
| 141 | + { |
| 142 | + match: func(err error) bool { |
| 143 | + return errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) |
| 144 | + }, |
| 145 | + convert: func(err error) usefulerror.UsefulError { |
| 146 | + return usefulerror.Useful(). |
| 147 | + WithCode(usefulerror.ErrCodeUnexpectedEOF). |
| 148 | + WithHumanError("Unexpected end of data"). |
| 149 | + WithHelp("Retry the download"). |
| 150 | + WithAdditionalHelp("This may indicate network instability"). |
| 151 | + Wrap(err) |
| 152 | + }, |
| 153 | + }, |
| 154 | +} |
| 155 | + |
| 156 | +// convertToUsefulError attempts to convert a regular error to a UsefulError |
| 157 | +// by analyzing the error chain for known error types. |
| 158 | +// Returns the original error wrapped in a generic UsefulError if no specific match is found. |
| 159 | +func convertToUsefulError(err error) usefulerror.UsefulError { |
| 160 | + if err == nil { |
| 161 | + return nil |
| 162 | + } |
| 163 | + |
| 164 | + if ue, ok := usefulerror.AsUsefulError(err); ok { |
| 165 | + return ue |
| 166 | + } |
| 167 | + |
| 168 | + for _, matcher := range errorMatchers { |
| 169 | + if matcher.match(err) { |
| 170 | + return matcher.convert(err) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return usefulerror.Useful(). |
| 175 | + WithCode(usefulerror.ErrCodeUnknown). |
| 176 | + WithHumanError(extractRootCause(err)). |
| 177 | + WithHelp("An unexpected error occurred."). |
| 178 | + Wrap(err) |
| 179 | +} |
| 180 | + |
| 181 | +// extractRootCause traverses the error chain and returns the innermost error message. |
| 182 | +// This provides a cleaner, more human-friendly message instead of the full error chain. |
| 183 | +func extractRootCause(err error) string { |
| 184 | + for { |
| 185 | + unwrapped := errors.Unwrap(err) |
| 186 | + if unwrapped == nil { |
| 187 | + return err.Error() |
| 188 | + } |
| 189 | + |
| 190 | + err = unwrapped |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// extractPathFromError attempts to extract a file path from path-related errors |
| 195 | +func extractPathFromError(err error) string { |
| 196 | + var pathErr *fs.PathError |
| 197 | + if errors.As(err, &pathErr) { |
| 198 | + return pathErr.Path |
| 199 | + } |
| 200 | + |
| 201 | + var linkErr *os.LinkError |
| 202 | + if errors.As(err, &linkErr) { |
| 203 | + return linkErr.Old |
| 204 | + } |
| 205 | + |
| 206 | + return "" |
| 207 | +} |
0 commit comments