Skip to content

Commit 893bb32

Browse files
committed
πŸ₯… Enhance error handling for dirty tree
Before, when the tree was checked for being dirty, the error message was duplicated around the code. Now, it is a registered known error
1 parent e84e84d commit 893bb32

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

β€Žsrc/controller/error.goβ€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
Err: errors.New("error reading input"),
1717
}
1818
errorWorkingTreeNotClean = GutError{
19-
Message: "I can't continue further because the working tree is not clean",
19+
Message: "I can't continue further because the working tree is not clean. Commit your changes with gut save",
2020
Code: 2,
2121
Err: errors.New("working tree not clean"),
2222
}
@@ -56,17 +56,21 @@ func getLinkForError(GutError GutError) string {
5656
//
5757
// This should be used when the error is known and can be resolved by the user
5858
func exitOnKnownError(typeOfError GutError, err error) {
59+
60+
// When the error is linked to the user input, we don't print the error message
61+
// because it's not useful
62+
if typeOfError.Code == 1 {
63+
os.Exit(1)
64+
return
65+
}
5966
// Print the error message to stderr
6067
fmt.Fprintln(os.Stderr, "")
6168
if err != nil {
6269
fmt.Fprintf(os.Stderr, color.RedString("%s (error: %s)\n"), color.RedString(typeOfError.Message), color.RedString(err.Error()))
63-
/* print.Message("%s (error: %s)", print.Error, typeOfError.Message, err.Error()) */
6470
} else {
6571
fmt.Fprintf(os.Stderr, "%s\n", color.RedString(typeOfError.Message))
66-
/* print.Message("%s", print.Error, typeOfError.Message) */
6772
}
6873
fmt.Fprintf(os.Stderr, "To resolve this issue, please follow the instructions on this page: %s\n", getLinkForError(typeOfError))
69-
/* print.Message("To resolve this issue, please follow the instructions on this page: %s", print.Optional, getLinkForError(typeOfError)) */
7074

7175
os.Exit(1)
7276
}

β€Žsrc/controller/merge.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Merge(cmd *cobra.Command, args []string) {
2727
exitOnError("Sorry, I can't check if the working directory is clean 😒", err)
2828
}
2929
if !clean {
30-
exitOnError("Uh oh, there are uncommitted changes. Please commit them before merging branches 😒", nil)
30+
exitOnKnownError(errorWorkingTreeNotClean, nil)
3131

3232
}
3333

β€Žsrc/controller/switch.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Switch(cmd *cobra.Command, args []string) {
5858
}
5959
// If not clean, we return an error
6060
if !clean {
61-
exitOnError("Sorry, you have uncommitted changes. Save them with \"gut save\" before going to another commit or you will lose them", nil)
61+
exitOnKnownError(errorWorkingTreeNotClean, nil)
6262
}
6363

6464
// Switch to the commit

β€Žsrc/controller/sync.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Sync(cmd *cobra.Command, args []string) {
3434
exitOnError("Sorry, I can't check if there are uncommited changes 😒", err)
3535
}
3636
if !clean {
37-
exitOnError("Uh oh, there are uncommited changes. Please commit them before syncing with `gut save`", nil)
37+
exitOnKnownError(errorWorkingTreeNotClean, nil)
3838
}
3939

4040
// Get the remote

0 commit comments

Comments
Β (0)