Skip to content

Commit

Permalink
Cooler errors (#35)
Browse files Browse the repository at this point in the history
* coolers errors
* adds more directive errors on what to do
Co-authored-by: Jake Cooper <jake.elijah.cooper@gmail.com>
  • Loading branch information
sdan authored Oct 20, 2020
1 parent 1992033 commit fc44c01
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
16 changes: 9 additions & 7 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

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

Expand All @@ -24,26 +25,27 @@ func (h *Handler) Status(ctx context.Context, req *entity.CommandRequest) error
}

project, err := h.ctrl.GetProject(ctx, projectCfg.Project)
if err != nil {
return err
}

if project != nil {
fmt.Printf("Project: %s\n", ui.MagentaText(project.Name))
fmt.Printf("Project: %s\n", ui.Bold(fmt.Sprint(ui.MagentaText(project.Name))))

if projectCfg.Environment != "" {
fmt.Printf("Environment: %s\n", ui.BlueText(getEnvironmentNameFromID(projectCfg.Environment, project.Environments)))
fmt.Printf("Environment: %s\n", ui.Bold(fmt.Sprint(ui.BlueText(getEnvironmentNameFromID(projectCfg.Environment, project.Environments)))))
} else {
fmt.Println("Not connected to an environment")
fmt.Println(ui.RedText("Not connected to an environment"))
}

if len(project.Plugins) > 0 {
fmt.Printf("Plugins:\n")
for i := range project.Plugins {
fmt.Printf("%s\n", ui.GrayText(project.Plugins[i].Name))
fmt.Printf("%s\n", ui.Bold(fmt.Sprint(ui.GrayText(project.Plugins[i].Name))))
}
}
} else if projectCfg.Project != "" {
fmt.Println("Project not found. Maybe you need to login?")
} else {
fmt.Println("Not connected to a project. Run railway init.")
fmt.Println(errors.ProjectConfigNotFound)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion configs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (c *Configs) GetProjectConfigs() (*entity.ProjectConfig, error) {
var cfg entity.ProjectConfig

if err := c.unmarshalConfig(c.projectConfigs, &cfg); err != nil {
return nil, errors.ProjectNotFound
return nil, errors.ProjectConfigNotFound
}
return &cfg, nil
}
Expand Down
10 changes: 5 additions & 5 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/pkg/browser"
configs "github.com/railwayapp/cli/configs"
"github.com/railwayapp/cli/entity"
"github.com/railwayapp/cli/errors"
"github.com/railwayapp/cli/ui"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (c *Controller) GetUser(ctx context.Context) (*entity.User, error) {
return nil, err
}
if userCfg.Token == "" {
return nil, errors.New("Not logged in")
return nil, errors.UserConfigNotFound
}
return c.gtwy.GetUser(ctx)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func (c *Controller) browserBasedLogin(ctx context.Context) (*entity.User, error
wg.Wait()

if code != returnedCode {
return nil, errors.New("Login failed")
return nil, errors.LoginFailed
}

err = c.cfg.SetUserConfigs(&entity.UserConfig{
Expand All @@ -143,7 +143,7 @@ func (c *Controller) pollForToken(ctx context.Context, code string) (string, err
token, err := c.gtwy.ConsumeLoginSession(ctx, code)

if err != nil {
return "", errors.New("Login failed")
return "", errors.LoginFailed
}

if token != "" {
Expand All @@ -154,7 +154,7 @@ func (c *Controller) pollForToken(ctx context.Context, code string) (string, err
time.Sleep(pollInterval)
}

return "", errors.New("Login timeout")
return "", errors.LoginTimeout
}

func (c *Controller) browserlessLogin(ctx context.Context) (*entity.User, error) {
Expand Down
15 changes: 8 additions & 7 deletions errors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
type RailwayError error

var (
UserConfigNotFound RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("Not logged in. Please run railway login.")))
ProjectConfigNotFound RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("Not connected to a project. Run railway init to get started.")))
ProjectNotFound RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("Project not found.")))
ProblemFetchingProjects RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("There was a problem fetching your projects")))
ProjectCreateFailed RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("There was a problem creating the project")))
ProductionTokenNotSet RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("RAILWAY_TOKEN environment variable not set")))
CommandNotSpecified RailwayError = errors.New(fmt.Sprintf("Specify a command to run in side the railway environment.\n%s %s", ui.Bold("railway run"), ui.MagentaText("<cmd>")))
UserConfigNotFound RailwayError = errors.New(fmt.Sprintf("%s\nRun %s", ui.RedText("Not logged in."), ui.Bold("railway login")))
ProjectConfigNotFound RailwayError = errors.New(fmt.Sprintf("%s\nRun %s to get plugged into a new or existing project.", ui.RedText("Project not found."), ui.Bold("railway init")))
ProblemFetchingProjects RailwayError = errors.New(fmt.Sprintf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem fetching your projects.")))
ProjectCreateFailed RailwayError = errors.New(fmt.Sprintf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem creating the project.")))
ProductionTokenNotSet RailwayError = errors.New(fmt.Sprintf("%s\nRun %s and head under `tokens` section. You can generate tokens to access Railway environment variables. Set that token in your environment as `RAILWAY_TOKEN=<insert token>` and you're all aboard!", ui.RedText("RAILWAY_TOKEN environment variable not set."), ui.Bold("railway open")))
CommandNotSpecified RailwayError = errors.New(fmt.Sprintf("%s\nRun %s", ui.RedText("Specify a command to run inside the railway environment."), ui.Bold("railway run <cmd>")))
LoginFailed RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("Login failed")))
LoginTimeout RailwayError = errors.New(fmt.Sprintf("%s", ui.RedText("Login timeout")))
)
2 changes: 1 addition & 1 deletion gateway/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (g *Gateway) GetProject(ctx context.Context, projectId string) (*entity.Pro
Project *entity.Project `json:"projectById"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectNotFound
return nil, errors.ProjectConfigNotFound
}
return resp.Project, nil
}
Expand Down

0 comments on commit fc44c01

Please sign in to comment.