From fb07d89c69caca7f87f6681bff670b54b95585d9 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 7 Oct 2022 15:18:28 +0400 Subject: [PATCH] Improved error messages (#281) --- cmd/link.go | 8 ++++---- configs/project.go | 2 +- configs/root.go | 6 +++++- errors/main.go | 4 +++- gateway/main.go | 17 +++++++++++++++-- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/cmd/link.go b/cmd/link.go index 87924f2bb..f1cfb4d70 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -14,9 +14,9 @@ func (h *Handler) Link(ctx context.Context, req *entity.CommandRequest) error { // projectID provided as argument arg := req.Args[0] - if (uuid.IsValidUUID(arg)) { + if uuid.IsValidUUID(arg) { project, err := h.ctrl.GetProject(ctx, arg) - if (err != nil) { + if err != nil { return err } @@ -24,7 +24,7 @@ func (h *Handler) Link(ctx context.Context, req *entity.CommandRequest) error { } project, err := h.ctrl.GetProjectByName(ctx, arg) - if (err != nil) { + if err != nil { return err } @@ -69,7 +69,7 @@ func (h *Handler) linkFromID(ctx context.Context, req *entity.CommandRequest) er } project, err := h.ctrl.GetProject(ctx, projectID) - if (err != nil) { + if err != nil { return err } diff --git a/configs/project.go b/configs/project.go index 722eb23dc..f5a73c375 100644 --- a/configs/project.go +++ b/configs/project.go @@ -24,7 +24,7 @@ func (c *Configs) GetProjectConfigs() (*entity.ProjectConfig, error) { userCfg, err := c.GetRootConfigs() if err != nil { - return nil, errors.ProjectConfigNotFound + return nil, errors.RootConfigNotFound } // lookup project in global config based on pwd diff --git a/configs/root.go b/configs/root.go index ae18e0dd1..b810811cd 100644 --- a/configs/root.go +++ b/configs/root.go @@ -2,7 +2,9 @@ package configs import ( "encoding/json" + "github.com/railwayapp/cli/errors" "io/ioutil" + "os" "github.com/railwayapp/cli/entity" ) @@ -10,7 +12,9 @@ import ( func (c *Configs) GetRootConfigs() (*entity.RootConfig, error) { var cfg entity.RootConfig b, err := ioutil.ReadFile(c.rootConfigs.configPath) - if err != nil { + if os.IsNotExist(err) { + return nil, errors.RootConfigNotFound + } else if err != nil { return nil, err } err = json.Unmarshal(b, &cfg) diff --git a/errors/main.go b/errors/main.go index 2a36fb187..6c78eec94 100644 --- a/errors/main.go +++ b/errors/main.go @@ -11,8 +11,10 @@ type RailwayError error // TEST var ( + RootConfigNotFound RailwayError = fmt.Errorf("Run %s to get started", ui.Bold("railway login")) UserConfigNotFound RailwayError = fmt.Errorf("%s\nRun %s", ui.RedText("Not logged in."), ui.Bold("railway login")) - ProjectConfigNotFound RailwayError = fmt.Errorf("%s. Tip: If you haven't, do railway login\nOtherwise, run %s to get plugged into a new project, or %s to get plugged into an existing project.", ui.RedText("Project not found."), ui.Bold("railway init"), ui.Bold("railway link")) + ProjectConfigNotFound RailwayError = fmt.Errorf("%s\nRun %s to create a new project, or %s to use an existing project", ui.RedText("Project not found"), ui.Bold("railway init"), ui.Bold("railway link")) + UserNotAuthorized RailwayError = fmt.Errorf("%s\nTry running %s", ui.RedText("Not authorized!"), ui.Bold("railway login")) ProjectTokenNotFound RailwayError = fmt.Errorf("%s\n", ui.RedText("Project token not found")) ProblemFetchingProjects RailwayError = fmt.Errorf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem fetching your projects.")) ProblemFetchingWritableGithubScopes RailwayError = fmt.Errorf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem fetching GitHub metadata.")) diff --git a/gateway/main.go b/gateway/main.go index 37f9cb884..12ae088c3 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -5,8 +5,11 @@ import ( "context" "encoding/json" "fmt" + errors2 "github.com/railwayapp/cli/errors" + "github.com/railwayapp/cli/ui" "io" "net/http" + "os" "strings" "time" @@ -166,10 +169,20 @@ func (r *GQLRequest) Run(ctx context.Context, resp interface{}) error { for i, err := range gr.Errors { messages[i] = err.Error() } + + errText := gr.Errors[0].Message if len(gr.Errors) > 1 { - return fmt.Errorf("%d Errors: %s", len(gr.Errors), strings.Join(messages, ", ")) + errText = fmt.Sprintf("%d Errors: %s", len(gr.Errors), strings.Join(messages, ", ")) + } + + // If any GQL responses return fail because unauthenticated, print an error telling the + // user to log in and exit immediately + if strings.Contains(errText, "Not Authorized") { + println(ui.AlertDanger(errors2.UserNotAuthorized.Error())) + os.Exit(1) } - return errors.New(gr.Errors[0].Message) + + return errors.New(errText) } return nil