From 776608dcce669f9e3bd2b76dd4a9355467398dd3 Mon Sep 17 00:00:00 2001 From: Dhruv Thakur <13575379+dhth@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:07:17 +0200 Subject: [PATCH] refactor: error handling --- cmd/root.go | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d903646..0a8ee41 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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. @@ -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) { @@ -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 @@ -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 {