|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "go.jetpack.io/devbox/internal/boxcli/usererr" |
| 8 | +) |
| 9 | + |
| 10 | +const errApplyNixDerivationString = "Error: apply Nix derivation:" |
| 11 | + |
| 12 | +var sshSessionErrorStrings = []string{ |
| 13 | + errApplyNixDerivationString, |
| 14 | +} |
| 15 | + |
| 16 | +// sshSessionErrors is a helper struct to collect errors from ssh sessions. |
| 17 | +// For performance and privacy it doesn't actually keep any content from the |
| 18 | +// sessions, but instead just keeps track of which errors were encountered. |
| 19 | +type sshSessionErrors struct { |
| 20 | + errors map[string]bool |
| 21 | +} |
| 22 | + |
| 23 | +var _ io.Writer = (*sshSessionErrors)(nil) |
| 24 | + |
| 25 | +func newSSHSessionErrors() *sshSessionErrors { |
| 26 | + return &sshSessionErrors{ |
| 27 | + errors: make(map[string]bool), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (s *sshSessionErrors) Write(p []byte) (n int, err error) { |
| 32 | + for _, errorString := range sshSessionErrorStrings { |
| 33 | + if strings.Contains(string(p), errorString) { |
| 34 | + s.errors[errorString] = true |
| 35 | + } |
| 36 | + } |
| 37 | + return len(p), nil |
| 38 | +} |
| 39 | + |
| 40 | +// cloudShellErrorHandler is a helper function to handle ssh errors that |
| 41 | +// may contain nix errors in them. For now being cautious and logging them |
| 42 | +// to Sentry even though they may be due to user action. |
| 43 | +func cloudShellErrorHandler(err error, sessionErrors *sshSessionErrors) error { |
| 44 | + if err == nil { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + // This usually on initial setup when running start_devbox_shell.sh |
| 49 | + if found := sessionErrors.errors[errApplyNixDerivationString]; found { |
| 50 | + return usererr.WithLoggedUserMessage( |
| 51 | + err, |
| 52 | + "Failed to apply Nix derivation. This can happen if your devbox (nix) "+ |
| 53 | + "packages don't exist or failed to build. Please check your "+ |
| 54 | + "devbox.json and try again", |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + // This can happen due to connection issues or any other unforeseen errors |
| 59 | + return usererr.WithLoggedUserMessage( |
| 60 | + err, |
| 61 | + "Your cloud shell terminated unexpectedly. Please check your connection "+ |
| 62 | + "and devbox.json and try again", |
| 63 | + ) |
| 64 | +} |
0 commit comments