Skip to content

Commit

Permalink
Improved error messages (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier authored Oct 7, 2022
1 parent 258a1cf commit fb07d89
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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
}

return h.setProject(ctx, project)
}

project, err := h.ctrl.GetProjectByName(ctx, arg)
if (err != nil) {
if err != nil {
return err
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion configs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion configs/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package configs

import (
"encoding/json"
"github.com/railwayapp/cli/errors"
"io/ioutil"
"os"

"github.com/railwayapp/cli/entity"
)

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)
Expand Down
4 changes: 3 additions & 1 deletion errors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand Down
17 changes: 15 additions & 2 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fb07d89

Please sign in to comment.