2
0

errors.go 1.7 KB

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