HandleExecuteError handles an error returned by RootCmd, returning the desired exit code. It contains user-friendly handling for common errors. NOTE (2025-03-27): This is a workaround for Cobra not supporting custom logic for printing the error from RunE.
(ch *cmdutil.Helper, err error)
| 192 | // It contains user-friendly handling for common errors. |
| 193 | // NOTE (2025-03-27): This is a workaround for Cobra not supporting custom logic for printing the error from RunE. |
| 194 | func HandleExecuteError(ch *cmdutil.Helper, err error) int { |
| 195 | if err == nil { |
| 196 | return 0 |
| 197 | } |
| 198 | |
| 199 | errMsg := err.Error() |
| 200 | if strings.Contains(errMsg, "org not found") { |
| 201 | ch.Println("Org not found. Run `rill org list` to see the orgs. Run `rill org switch` to default org.") |
| 202 | } else if strings.Contains(errMsg, "project not found") { |
| 203 | ch.Println("Project not found. Run `rill project list` to check the list of projects.") |
| 204 | } else if strings.Contains(errMsg, "auth token not found") { |
| 205 | ch.Println("Auth token is invalid/expired. Login again with `rill login`.") |
| 206 | } else if strings.Contains(errMsg, "not authenticated as a user") { |
| 207 | ch.Println("Please log in or sign up for Rill with `rill login`.") |
| 208 | } else { |
| 209 | if s, ok := status.FromError(err); ok { |
| 210 | // rpc error |
| 211 | ch.Printf("Error: %s (%v)\n", s.Message(), s.Code()) |
| 212 | } else { |
| 213 | ch.Printf("Error: %s\n", err.Error()) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return 1 |
| 218 | } |