Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

more error handling improvements, replace panics

Dane Schneider54730e9a02c1
diff --git a/app/cli/auth/auth.go b/app/cli/auth/auth.go
index dc70532e..178d004b 100644
--- a/app/cli/auth/auth.go
+++ b/app/cli/auth/auth.go
@@ -15,7 +15,7 @@ func MustResolveAuthWithOrg() {
func MustResolveAuth(requireOrg bool) {
if apiClient == nil {
- panic(fmt.Errorf("error resolving auth: api client not set"))
+ term.OutputErrorAndExit("error resolving auth: api client not set")
}
// load HomeAuthPath file into ClientAuth struct
@@ -26,19 +26,19 @@ func MustResolveAuth(requireOrg bool) {
err = promptInitialAuth()
if err != nil {
- panic(fmt.Errorf("error resolving auth: %v", err))
+ term.OutputErrorAndExit("error resolving auth: %v", err)
}
return
} else {
- panic(fmt.Errorf("error reading auth.json: %v", err))
+ term.OutputErrorAndExit("error reading auth.json: %v", err)
}
}
var auth types.ClientAuth
err = json.Unmarshal(bytes, &auth)
if err != nil {
- panic(fmt.Errorf("error unmarshalling auth.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling auth.json: %v", err)
}
Current = &auth
diff --git a/app/cli/changes_tui/model.go b/app/cli/changes_tui/model.go
index 9515664d..61021def 100644
--- a/app/cli/changes_tui/model.go
+++ b/app/cli/changes_tui/model.go
@@ -5,6 +5,7 @@ import (
"log"
"plandex/api"
"plandex/lib"
+ "plandex/term"
"github.com/charmbracelet/bubbles/help"
bubbleKey "github.com/charmbracelet/bubbles/key"
@@ -59,7 +60,7 @@ func initialModel() *changesUIModel {
if apiErr != nil {
err := fmt.Errorf("error getting current plan state: %v", apiErr.Msg)
log.Println(err)
- panic(err)
+ term.OutputErrorAndExit(err.Error())
}
initialState := changesUIModel{
diff --git a/app/cli/changes_tui/selection.go b/app/cli/changes_tui/selection.go
index 7d02aeff..bcb95ad3 100644
--- a/app/cli/changes_tui/selection.go
+++ b/app/cli/changes_tui/selection.go
@@ -3,6 +3,7 @@ package changes_tui
import (
"fmt"
"log"
+ "plandex/term"
"github.com/plandex/plandex/shared"
)
@@ -64,7 +65,7 @@ outer:
if err != nil {
err = fmt.Errorf("error getting current plan state: %v", err)
log.Println(err)
- panic(err)
+ term.OutputErrorAndExit(err.Error())
}
m.selectionInfo = &selectionInfo{
diff --git a/app/cli/fs/fs.go b/app/cli/fs/fs.go
index cca68cd5..2b62bd25 100644
--- a/app/cli/fs/fs.go
+++ b/app/cli/fs/fs.go
@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "plandex/term"
"plandex/types"
"strings"
"sync"
@@ -29,12 +30,12 @@ func init() {
var err error
Cwd, err = os.Getwd()
if err != nil {
- panic(err)
+ term.OutputErrorAndExit("Error getting current working directory: %v", err)
}
home, err := os.UserHomeDir()
if err != nil {
- panic("Couldn't find home dir:" + err.Error())
+ term.OutputErrorAndExit("Couldn't find home dir: %v", err.Error())
}
HomeDir = home
@@ -47,7 +48,7 @@ func init() {
// Create the home plandex directory if it doesn't exist
err = os.MkdirAll(HomePlandexDir, os.ModePerm)
if err != nil {
- panic(err)
+ term.OutputErrorAndExit(err.Error())
}
CacheDir = filepath.Join(HomePlandexDir, "cache")
@@ -56,11 +57,11 @@ func init() {
err = os.MkdirAll(filepath.Join(CacheDir, "tiktoken"), os.ModePerm)
if err != nil {
- panic(err)
+ term.OutputErrorAndExit(err.Error())
}
err = os.Setenv("TIKTOKEN_CACHE_DIR", CacheDir)
if err != nil {
- panic(err)
+ term.OutputErrorAndExit(err.Error())
}
PlandexDir = findPlandex(Cwd)
@@ -357,7 +358,7 @@ func GetParentProjectIdsWithPaths() ([][2]string, error) {
err = json.Unmarshal(bytes, &settings)
if err != nil {
- panic(fmt.Errorf("error unmarshalling project.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
}
projectId := string(settings.Id)
@@ -411,7 +412,7 @@ func GetChildProjectIdsWithPaths(ctx context.Context) ([][2]string, error) {
err = json.Unmarshal(bytes, &settings)
if err != nil {
- panic(fmt.Errorf("error unmarshalling project.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
}
projectId := string(settings.Id)
diff --git a/app/cli/lib/context_update.go b/app/cli/lib/context_update.go
index d9f22cb2..5e44fbb3 100644
--- a/app/cli/lib/context_update.go
+++ b/app/cli/lib/context_update.go
@@ -26,7 +26,7 @@ func MustCheckOutdatedContext(cancelOpt, quiet bool, maybeContexts []*shared.Con
outdatedRes, err := CheckOutdatedContext(maybeContexts)
if err != nil {
term.StopSpinner()
- panic(fmt.Errorf("failed to check outdated context: %s", err))
+ term.OutputErrorAndExit("failed to check outdated context: %s", err)
}
term.StopSpinner()
@@ -96,7 +96,7 @@ func MustCheckOutdatedContext(cancelOpt, quiet bool, maybeContexts []*shared.Con
}
if err != nil {
- panic(fmt.Errorf("failed to get user input: %s", err))
+ term.OutputErrorAndExit("failed to get user input: %s", err)
}
if confirmed {
diff --git a/app/cli/lib/current.go b/app/cli/lib/current.go
index d5434ba7..d540ff7d 100644
--- a/app/cli/lib/current.go
+++ b/app/cli/lib/current.go
@@ -8,6 +8,7 @@ import (
"path/filepath"
"plandex/api"
"plandex/fs"
+ "plandex/term"
"plandex/types"
"github.com/fatih/color"
@@ -36,7 +37,7 @@ func resolveProject(mustResolve, shouldCreate bool) {
if fs.PlandexDir == "" && mustResolve && shouldCreate {
_, _, err := fs.FindOrCreatePlandex()
if err != nil {
- panic(fmt.Errorf("error finding or creating plandex: %v", err))
+ term.OutputErrorAndExit("error finding or creating plandex: %v", err)
}
}
@@ -61,21 +62,21 @@ func resolveProject(mustResolve, shouldCreate bool) {
log.Println("Initializing project")
mustInitProject()
} else if err != nil {
- panic(fmt.Errorf("error checking if project.json exists: %v", err))
+ term.OutputErrorAndExit("error checking if project.json exists: %v", err)
}
// read project.json
bytes, err := os.ReadFile(path)
if err != nil {
- panic(fmt.Errorf("error reading project.json: %v", err))
+ term.OutputErrorAndExit("error reading project.json: %v", err)
}
var settings types.CurrentProjectSettings
err = json.Unmarshal(bytes, &settings)
if err != nil {
- panic(fmt.Errorf("error unmarshalling project.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
}
CurrentProjectId = settings.Id
@@ -86,7 +87,7 @@ func resolveProject(mustResolve, shouldCreate bool) {
err = os.MkdirAll(HomeCurrentProjectDir, os.ModePerm)
if err != nil {
- panic(fmt.Errorf("error creating project dir: %v", err))
+ term.OutputErrorAndExit("error creating project dir: %v", err)
}
MustLoadCurrentPlan()
@@ -94,7 +95,7 @@ func resolveProject(mustResolve, shouldCreate bool) {
func MustLoadCurrentPlan() {
if CurrentProjectId == "" {
- panic("No current project")
+ term.OutputErrorAndExit("No current project")
}
// Check if the file exists
@@ -103,19 +104,19 @@ func MustLoadCurrentPlan() {
if os.IsNotExist(err) {
return
} else if err != nil {
- panic(fmt.Errorf("error checking if current_plan.json exists: %v", err))
+ term.OutputErrorAndExit("error checking if current_plan.json exists: %v", err)
}
// Read the contents of the file
fileBytes, err := os.ReadFile(HomeCurrentPlanPath)
if err != nil {
- panic(fmt.Errorf("error reading current_plan.json: %v", err))
+ term.OutputErrorAndExit("error reading current_plan.json: %v", err)
}
var currentPlan types.CurrentPlanSettings
err = json.Unmarshal(fileBytes, &currentPlan)
if err != nil {
- panic(fmt.Errorf("error unmarshalling current_plan.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling current_plan.json: %v", err)
}
CurrentPlanId = currentPlan.Id
@@ -124,14 +125,14 @@ func MustLoadCurrentPlan() {
err = loadCurrentBranch()
if err != nil {
- panic(fmt.Errorf("error loading current branch: %v", err))
+ term.OutputErrorAndExit("error loading current branch: %v", err)
}
if CurrentBranch == "" {
err = WriteCurrentBranch("main")
if err != nil {
- panic(fmt.Errorf("error setting current branch: %v", err))
+ term.OutputErrorAndExit("error setting current branch: %v", err)
}
}
}
@@ -156,13 +157,13 @@ func loadCurrentBranch() error {
fileBytes, err := os.ReadFile(path)
if err != nil {
- panic(fmt.Errorf("error reading settings.json: %v", err))
+ term.OutputErrorAndExit("error reading settings.json: %v", err)
}
var settings types.PlanSettings
err = json.Unmarshal(fileBytes, &settings)
if err != nil {
- panic(fmt.Errorf("error unmarshalling settings.json: %v", err))
+ term.OutputErrorAndExit("error unmarshalling settings.json: %v", err)
}
CurrentBranch = settings.Branch
@@ -175,7 +176,7 @@ func mustInitProject() {
res, apiErr := api.Client.CreateProject(shared.CreateProjectRequest{Name: filepath.Base(fs.ProjectRoot)})
if apiErr != nil {
- panic(fmt.Errorf("error creating project: %v", apiErr.Msg))
+ term.OutputErrorAndExit("error creating project: %v", apiErr.Msg)
}
log.Println("Project created:", res.Id)
@@ -189,13 +190,13 @@ func mustInitProject() {
})
if err != nil {
- panic(fmt.Errorf("error marshalling project settings: %v", err))
+ term.OutputErrorAndExit("error marshalling project settings: %v", err)
}
err = os.WriteFile(path, bytes, os.ModePerm)
if err != nil {
- panic(fmt.Errorf("error writing project.json: %v", err))
+ term.OutputErrorAndExit("error writing project.json: %v", err)
}
log.Println("Wrote project.json")
@@ -205,7 +206,7 @@ func mustInitProject() {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
- panic(fmt.Errorf("error creating project dir: %v", err))
+ term.OutputErrorAndExit("error creating project dir: %v", err)
}
path = filepath.Join(dir, "current_plan.json")
@@ -214,13 +215,13 @@ func mustInitProject() {
})
if err != nil {
- panic(fmt.Errorf("error marshalling plan settings: %v", err))
+ term.OutputErrorAndExit("error marshalling plan settings: %v", err)
}
err = os.WriteFile(path, bytes, os.ModePerm)
if err != nil {
- panic(fmt.Errorf("error writing current_plan.json: %v", err))
+ term.OutputErrorAndExit("error writing current_plan.json: %v", err)
}
log.Println("Wrote current_plan.json")
diff --git a/app/cli/term/help.go b/app/cli/term/help.go
index f98b8672..f6e79636 100644
--- a/app/cli/term/help.go
+++ b/app/cli/term/help.go
@@ -39,6 +39,7 @@ var CmdDesc = map[string][2]string{
"ps": {"", "list active and recently finished plan streams"},
"stop": {"", "stop an active plan stream"},
"connect": {"conn", "connect to an active plan stream"},
+ "sign-in": {"", "sign in to an existing account or create a new one"},
}
func PrintCmds(prefix string, cmds ...string) {
@@ -98,6 +99,9 @@ func PrintCustomHelp() {
fmt.Fprintln(builder)
color.New(color.Bold, color.BgMagenta).Fprintln(builder, " Getting Started ")
+ fmt.Fprintf(builder, " Create a new plan in your project's root directory with %s\n\n", color.New(color.Bold, color.BgCyan).Sprint(" plandex new "))
+
+ color.New(color.Bold, color.BgMagenta).Fprintln(builder, " Key Commands ")
printCmds(builder, " ", []color.Attribute{color.Bold}, "new", "load", "tell", "changes", "apply")
fmt.Fprintln(builder)
@@ -131,6 +135,11 @@ func PrintCustomHelp() {
color.New(color.Bold, color.BgBlue).Fprintln(builder, " AI Models ")
printCmds(builder, " ", []color.Attribute{color.Bold}, "models", "set-model")
+ fmt.Fprintln(builder)
+
+ color.New(color.Bold, color.BgBlue).Fprintln(builder, " Accounts ")
+ printCmds(builder, " ", []color.Attribute{color.Bold}, "sign-in")
+ fmt.Fprintln(builder)
fmt.Print(builder.String())
}