Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
cmd: update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aavshr committed Sep 15, 2023
1 parent 2d15ddf commit 165bf1f
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 200 deletions.
13 changes: 6 additions & 7 deletions cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func newCmdEnv() *cobra.Command {
var err error
projectID, err = runtime.GetProjectID(projectDir)
if err != nil {
utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to get project id: %w", err)
}
}

Expand Down Expand Up @@ -89,7 +88,7 @@ func cmdEnvGetFn(microName string, file string, projectID string) error {

err = godotenv.Write(envMap, file)
if err != nil {
return fmt.Errorf("Failed to write to `%s` env file: %s", file, err)
return fmt.Errorf("failed to write to `%s` env file: %w", file, err)
}

utils.Logger.Printf("%s Wrote %d environment variables from the micro `%s` to the file `%s`",
Expand All @@ -111,12 +110,12 @@ func cmdEnvSetFn(microName string, file string, projectID string) error {

data, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("Failed to read `%s` env file: %s", file, err)
return fmt.Errorf("failed to read `%s` env file: %w", file, err)
}

envMap, err := godotenv.UnmarshalBytes(data)
if err != nil {
return fmt.Errorf("Failed to parse `%s` env file: %s", file, err)
return fmt.Errorf("failed to parse `%s` env file: %w", file, err)
}

// update the values in-place
Expand Down Expand Up @@ -165,8 +164,8 @@ func cmdEnvGetMicro(microName string, devInstance *api.AppInstance) (*api.AppIns
}

if microName == "" {
return nil, fmt.Errorf("Please provide a valid micro name with the `--micro` flag")
return nil, fmt.Errorf("please provide a valid micro name with the `--micro` flag")
} else {
return nil, fmt.Errorf("Micro '%s' not found in this project", microName)
return nil, fmt.Errorf("micro '%s' not found in this project", microName)
}
}
14 changes: 5 additions & 9 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ The cli will start one process for each of your micros, then expose a single enp
if !cmd.Flags().Changed("id") {
projectID, err = runtime.GetProjectID(projectDir)
if err != nil {
utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to get proejct id: %w", err)
}
}

Expand Down Expand Up @@ -126,17 +125,14 @@ func dev(projectDir string, projectID string, host string, port int, open bool)
routeDir := filepath.Join(projectDir, ".space", "micros")
spacefile, err := spacefile.LoadSpacefile(projectDir)
if err != nil {
utils.Logger.Printf("%s Failed to parse Spacefile: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to parse Spacefile: %w", err)
}

projectKey, err := utils.GenerateDataKeyIfNotExists(projectID)

addr := fmt.Sprintf("%s:%d", host, port)
if err != nil {
utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation)
return err
return fmt.Errorf("failed to generate project key: %w", err)
}
addr := fmt.Sprintf("%s:%d", host, port)

utils.Logger.Printf("\n%s Checking for running micros...", emoji.Eyes)
var stoppedMicros []*types.Micro
Expand Down Expand Up @@ -220,7 +216,7 @@ func dev(projectDir string, projectID string, host string, port int, open bool)
defer wg.Done()
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
utils.Logger.Println("proxy error", err)
utils.StdErrLogger.Println("proxy error", err)
}
}()

Expand Down
6 changes: 2 additions & 4 deletions cmd/dev_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ The micros will be automatically discovered and proxied to.`,
if !cmd.Flags().Changed("port") {
port, err = GetFreePort(utils.DevPort)
if err != nil {
utils.Logger.Printf("%s Failed to get free port: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to get free port: %w", err)
}
}

Expand Down Expand Up @@ -80,8 +79,7 @@ func devProxy(projectDir string, host string, port int, open bool) error {

projectKey, err := utils.GenerateDataKeyIfNotExists(meta.ID)
if err != nil {
utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation)
return err
return fmt.Errorf("failed to generate project key: %w", err)
}

reverseProxy := proxy.NewReverseProxy(projectKey, meta.ID, meta.Name, meta.Alias)
Expand Down
14 changes: 5 additions & 9 deletions cmd/dev_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Make sure that the corresponding micro is running before triggering the action.`
func triggerScheduledAction(projectDir string, actionID string) (err error) {
spacefile, err := spacefile.LoadSpacefile(projectDir)
if err != nil {
utils.Logger.Printf("%s failed to parse Spacefile: %s", emoji.X, err.Error())
return fmt.Errorf("failed to parse Spacefile: %w", err)
}
routeDir := filepath.Join(projectDir, ".space", "micros")

Expand All @@ -157,7 +157,7 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) {
port, err := getMicroPort(micro, routeDir)
if err != nil {
upCommand := fmt.Sprintf("space dev up %s", micro.Name)
utils.Logger.Printf("%smicro %s is not running, to start it run:", emoji.X, styles.Green(micro.Name))
utils.Logger.Printf("%s Micro %s is not running, to start it run:", emoji.X, styles.Green(micro.Name))
utils.Logger.Printf("L %s", styles.Blue(upCommand))
return err
}
Expand All @@ -180,8 +180,7 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) {

res, err := http.Post(actionEndpoint, "application/json", bytes.NewReader(body))
if err != nil {
utils.Logger.Printf("\n%s failed to trigger action: %s", emoji.X, err.Error())
return err
return fmt.Errorf("failed to trigger action: %w", err)
}
defer res.Body.Close()

Expand All @@ -193,14 +192,11 @@ func triggerScheduledAction(projectDir string, actionID string) (err error) {
io.Copy(os.Stdout, res.Body)

if res.StatusCode >= 400 {
utils.Logger.Printf("\n\nL %s", styles.Error("failed to trigger action"))
return err
return fmt.Errorf("\n\nL failed to trigger action")
}
utils.Logger.Printf("\n\nL Action triggered successfully!")
return nil
}
}

utils.Logger.Printf("\n%saction `%s` not found", emoji.X, actionID)
return fmt.Errorf("\n%saction `%s` not found", emoji.X, actionID)
return fmt.Errorf("\n%s action `%s` not found", emoji.X, actionID)
}
11 changes: 3 additions & 8 deletions cmd/dev_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ func newCmdDevUp() *cobra.Command {
if err != nil {
return err
}

}

if !cmd.Flags().Changed("port") {
port, err = GetFreePort(utils.DevPort + 1)
if err != nil {
utils.Logger.Printf("%s Failed to get free port: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to get free port: %w", err)
}
}

Expand All @@ -68,13 +66,12 @@ func devUp(projectDir string, projectId string, port int, microName string, open

spacefile, err := spacefile.LoadSpacefile(projectDir)
if err != nil {
return err
return fmt.Errorf("failed to parse Spacefile: %w", err)
}

projectKey, err := utils.GenerateDataKeyIfNotExists(projectId)
if err != nil {
utils.Logger.Printf("%s Error generating the project key", emoji.ErrorExclamation)
return err
return fmt.Errorf("failed to generate project key: %w", err)
}

for _, micro := range spacefile.Micros {
Expand Down Expand Up @@ -128,7 +125,5 @@ func devUp(projectDir string, projectId string, port int, microName string, open
command.Wait()
return nil
}

utils.Logger.Printf("micro %s not found", microName)
return fmt.Errorf("micro %s not found", microName)
}
8 changes: 3 additions & 5 deletions cmd/exec.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/deta/space/cmd/utils"
"github.com/deta/space/internal/runtime"
"github.com/deta/space/pkg/components/emoji"
"github.com/spf13/cobra"
)

Expand All @@ -26,8 +26,7 @@ The data key will be automatically injected into the command's environment.`,
cwd, _ := os.Getwd()
projectID, err = runtime.GetProjectID(cwd)
if err != nil {
utils.Logger.Printf("project id not provided and could not be inferred from current working directory")
return err
return fmt.Errorf("project id not provided and could not be inferred from current working directory")
}
}

Expand All @@ -49,8 +48,7 @@ func execRun(projectID string, args []string) error {

projectKey, err := utils.GenerateDataKeyIfNotExists(projectID)
if err != nil {
utils.Logger.Printf("%sError generating data key: %s\n", emoji.ErrorExclamation, err.Error())
return err
return fmt.Errorf("failed to generate data key: %w", err)
}

name := args[0]
Expand Down
9 changes: 3 additions & 6 deletions cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,15 @@ func link(projectDir string, projectID string) error {
return err
}
if errors.Is(err, api.ErrProjectNotFound) {
utils.Logger.Println(styles.Errorf("%s No project found. Please provide a valid Project ID.", emoji.ErrorExclamation))
return err
return fmt.Errorf("no project found, please provide a valid project id")
}

utils.Logger.Println(styles.Errorf("%s Failed to link project, %s", emoji.ErrorExclamation, err.Error()))
return err
return fmt.Errorf("failed to get project details, %w", err)
}

err = runtime.StoreProjectMeta(projectDir, &runtime.ProjectMeta{ID: projectRes.ID, Name: projectRes.Name, Alias: projectRes.Alias})
if err != nil {
utils.Logger.Printf("failed to link project: %s", err)
return err
return fmt.Errorf("failed to store project metadata locally, %w", err)
}

utils.Logger.Println(styles.Greenf("%s Project", emoji.Link), styles.Pink(projectRes.Name), styles.Green("was linked!"))
Expand Down
15 changes: 5 additions & 10 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/deta/space/cmd/utils"
"github.com/deta/space/internal/api"
"github.com/deta/space/internal/auth"
"github.com/deta/space/pkg/components/emoji"
"github.com/deta/space/pkg/components/styles"
"github.com/deta/space/pkg/components/text"
"github.com/spf13/cobra"
Expand All @@ -30,8 +29,7 @@ func newCmdLogin() *cobra.Command {
if withToken {
input, err := io.ReadAll(os.Stdin)
if err != nil {
utils.Logger.Println("failed to read access token from standard input")
return err
return fmt.Errorf("failed to read access token from standard input, %w", err)
}

accessToken = strings.TrimSpace(string(input))
Expand All @@ -44,8 +42,7 @@ func newCmdLogin() *cobra.Command {
}

if err := login(accessToken); err != nil {
utils.Logger.Printf(styles.Errorf("%s Failed to login: %v", emoji.ErrorExclamation, err))
return err
return fmt.Errorf("failed to login, %w", err)
}

return nil
Expand Down Expand Up @@ -84,16 +81,14 @@ func login(accessToken string) (err error) {

if err != nil {
if errors.Is(err, auth.ErrInvalidAccessToken) {
utils.Logger.Printf(styles.Errorf("%s Invalid access token. Please generate a valid token from your Space settings.", emoji.ErrorExclamation))
return fmt.Errorf("invalid access token")
return fmt.Errorf("invalid access token, please generate a valid token from your Space settings")
}
utils.Logger.Printf(styles.Errorf("%s Failed to validate access token: %v", emoji.ErrorExclamation, err))
return fmt.Errorf("failed to validate access token: %w", err)
return fmt.Errorf("failed to validate access token, %w", err)
}

err = auth.StoreAccessToken(accessToken)
if err != nil {
return fmt.Errorf("failed to store access token: %w", err)
return fmt.Errorf("failed to store access token, %w", err)
}

utils.Logger.Println(styles.Green("👍 Login Successful!"))
Expand Down
12 changes: 4 additions & 8 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func newCmdNew() *cobra.Command {
if !cmd.Flags().Changed("name") {
abs, err := filepath.Abs(projectDir)
if err != nil {
utils.Logger.Printf("%sError getting absolute path of project directory: %s", styles.ErrorExclamation, err.Error())
return err
return fmt.Errorf("failed to get absolute path of project directory: %w", err)
}

name := filepath.Base(abs)
Expand Down Expand Up @@ -149,8 +148,7 @@ func newProject(projectDir, projectName string, blankProject bool) error {
if _, err := os.Stat(spaceFilePath); errors.Is(err, os.ErrNotExist) {
err := createSpacefile(projectDir, projectName, blankProject)
if err != nil {
utils.Logger.Printf("failed to create spacefile: %s", err)
return err
return fmt.Errorf("failed to create Spacefile, %w", err)
}
}

Expand All @@ -161,13 +159,11 @@ func newProject(projectDir, projectName string, blankProject bool) error {
utils.Logger.Println(utils.LoginInfo())
return err
}
utils.Logger.Printf("failed to create project: %s", err)
return err
return fmt.Errorf("failed to create a project, %w", err)
}

if err := runtime.StoreProjectMeta(projectDir, meta); err != nil {
utils.Logger.Printf("failed to save project meta, %s", err)
return err
return fmt.Errorf("failed to save project metadata locally, %w", err)
}

utils.Logger.Println(styles.Greenf("\nProject %s created successfully!", projectName))
Expand Down
7 changes: 2 additions & 5 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/deta/space/cmd/utils"
"github.com/deta/space/internal/runtime"
"github.com/deta/space/pkg/components/emoji"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,15 +34,13 @@ func open(cmd *cobra.Command, args []string) error {
var err error
projectID, err = runtime.GetProjectID(projectDir)
if err != nil {
utils.Logger.Printf("%s Failed to get project id: %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to get the project id, %w", err)
}
}

utils.Logger.Printf("Opening project in default browser...\n")
if err := browser.OpenURL(fmt.Sprintf("%s/%s", utils.BuilderUrl, projectID)); err != nil {
utils.Logger.Printf("%s Failed to open browser window %s", emoji.ErrorExclamation, err)
return err
return fmt.Errorf("failed to open a browser window, %w", err)
}

return nil
Expand Down
Loading

0 comments on commit 165bf1f

Please sign in to comment.