errors.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package cmd
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "github.com/fatih/color"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. )
  9. var ErrNotLoggedIn error = errors.New("You are not logged in.")
  10. var ErrCannotConnect error = errors.New("Unable to connect to the Porter server.")
  11. func checkLoginAndRun(args []string, runner func(user *api.AuthCheckResponse, client *api.Client, args []string) error) error {
  12. client := GetAPIClient(config)
  13. user, err := client.AuthCheck(context.Background())
  14. if err != nil {
  15. red := color.New(color.FgRed)
  16. if strings.Contains(err.Error(), "403") {
  17. red.Print("You are not logged in. Log in using \"porter auth login\"\n")
  18. return ErrNotLoggedIn
  19. } else if strings.Contains(err.Error(), "connection refused") {
  20. red.Printf("Unable to connect to the Porter server at %s\n", config.Host)
  21. red.Print("To set a different host, run \"porter config set-host [HOST]\"\n")
  22. red.Print("To start a local server, run \"porter server start\"\n")
  23. return ErrCannotConnect
  24. }
  25. red.Printf("Error: %v\n", err.Error())
  26. return err
  27. }
  28. err = runner(user, client, args)
  29. if err != nil {
  30. red := color.New(color.FgRed)
  31. if strings.Contains(err.Error(), "403") {
  32. red.Print("You do not have the necessary permissions to view this resource")
  33. return nil
  34. } else if strings.Contains(err.Error(), "connection refused") {
  35. red.Printf("Unable to connect to the Porter server at %s\n", config.Host)
  36. red.Print("To set a different host, run \"porter config set-host [HOST]\"")
  37. red.Print("To start a local server, run \"porter server start\"")
  38. return nil
  39. }
  40. red.Printf("Error: %v\n", err.Error())
  41. return err
  42. }
  43. return nil
  44. }