Skip to content

Commit

Permalink
refactor: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Aug 24, 2024
1 parent 26e1cda commit 776608d
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,23 @@ const (
)

var (
errConfigFileExtIncorrect = errors.New("config file must be a TOML file")
errConfigFileDoesntExist = errors.New("config file does not exist")
errDBFileExtIncorrect = errors.New("db file needs to end with .db")

errMaxImportLimitExceeded = errors.New("import limit exceeded")
errNothingToImport = errors.New("nothing to import")

errListDensityIncorrect = errors.New("list density is incorrect; valid values: compact/spacious")

errCouldntGetHomeDir = errors.New("couldn't get home directory")
errConfigFileExtIncorrect = errors.New("config file must be a TOML file")
errConfigFileDoesntExist = errors.New("config file does not exist")
errDBFileExtIncorrect = errors.New("db file needs to end with .db")
errMaxImportLimitExceeded = errors.New("import limit exceeded")
errNothingToImport = errors.New("nothing to import")
errListDensityIncorrect = errors.New("list density is incorrect; valid values: compact/spacious")
errCouldntCreateDBDirectory = errors.New("couldn't create directory for database")
errCouldntCreateDB = errors.New("couldn't create database")
errCouldntInitializeDB = errors.New("couldn't initialize database")
errCouldntOpenDB = errors.New("couldn't open database")
errCouldntSetupGuide = errors.New("couldn't set up guided walkthrough")

//go:embed assets/updates.txt
updateContents string

reportIssueMsg = fmt.Sprintf("Let %s know about this error via %s.", author, repoIssuesURL)
reportIssueMsg = fmt.Sprintf("This isn't supposed to happen; let %s know about this error via \n%s.", author, repoIssuesURL)
maxImportNumMsg = fmt.Sprintf(`A maximum of %d tasks that can be imported at a time.
Archive/Delete tasks that are not active using ctrl+d/ctrl+x.
Expand All @@ -68,14 +67,22 @@ Archive/Delete tasks that are not active using ctrl+d/ctrl+x.

func Execute(version string) error {
rootCmd, err := NewRootCommand()

rootCmd.Version = version
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
switch {
case errors.Is(err, errCouldntGetHomeDir):
fmt.Printf("\n%s\n", reportIssueMsg)
}
return err
}
rootCmd.Version = version

return rootCmd.Execute()
err = rootCmd.Execute()
switch {
case errors.Is(err, errCouldntSetupGuide):
fmt.Printf("\n%s\n", reportIssueMsg)
}
return err
}

func setupDB(dbPathFull string) (*sql.DB, error) {
Expand Down Expand Up @@ -360,10 +367,7 @@ Sorry for breaking the upgrade step!
PreRunE: func(_ *cobra.Command, _ []string) error {
guideErr := insertGuideTasks(db)
if guideErr != nil {
return fmt.Errorf(`Failed to set up a guided walkthrough.
%s
Error: %w`, reportIssueMsg, guideErr)
return fmt.Errorf("%w: %s", errCouldntSetupGuide, guideErr.Error())
}

return nil
Expand Down Expand Up @@ -405,12 +409,7 @@ Error: %w`, reportIssueMsg, guideErr)
var configPathAdditionalCxt, dbPathAdditionalCxt string
hd, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf(`Couldn't get your home directory. This is a fatal error;
use --dbpath to specify database path manually
%s
Error: %w`, reportIssueMsg, err)
return nil, fmt.Errorf("%w: %s", errCouldntGetHomeDir, err.Error())
}

switch ros {
Expand Down

0 comments on commit 776608d

Please sign in to comment.