-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Git package error handling (#8345)
- Loading branch information
Showing
7 changed files
with
101 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package git | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/treeverse/lakefs/pkg/git/errors" | ||
) | ||
|
||
func HandleOutput(out string, err error) (string, error) { | ||
lowerOut := strings.ToLower(out) | ||
switch { | ||
case err == nil: | ||
return strings.TrimSpace(out), nil | ||
case strings.Contains(lowerOut, "not a git repository"): | ||
return "", errors.ErrNotARepository | ||
case strings.Contains(lowerOut, "remote not found"): | ||
return "", errors.ErrRemoteNotFound | ||
default: | ||
return "", fmt.Errorf("%s: %w", out, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package internal_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
giterror "github.com/treeverse/lakefs/pkg/git/errors" | ||
"github.com/treeverse/lakefs/pkg/git/internal" | ||
) | ||
|
||
var testErr = errors.New("this is a test generated error") | ||
|
||
func TestHandleOutput(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Output string | ||
Error error | ||
ExpectedError error | ||
}{ | ||
{ | ||
Name: "not git repository", | ||
Output: "fatal: not a git repository (or any of the parent directories): .git", | ||
Error: testErr, | ||
ExpectedError: giterror.ErrNotARepository, | ||
}, | ||
{ | ||
Name: "not a git repository - Mount", | ||
Output: "fatal: Not a git repository (or any parent up to mount point /home/my_home)\nStopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", | ||
Error: testErr, | ||
ExpectedError: giterror.ErrNotARepository, | ||
}, | ||
{ | ||
Name: "other error", | ||
Output: "Some other error happened", | ||
Error: testErr, | ||
ExpectedError: testErr, | ||
}, | ||
{ | ||
Name: "remote not found", | ||
Output: "prefix, Remote nOt founD, suffix", | ||
Error: testErr, | ||
ExpectedError: giterror.ErrRemoteNotFound, | ||
}, | ||
{ | ||
Name: "no error", | ||
Output: "This is a valid response", | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
str, err := internal.HandleOutput(tt.Output, tt.Error) | ||
require.ErrorIs(t, err, tt.ExpectedError) | ||
if tt.ExpectedError != nil { | ||
require.Equal(t, "", str) | ||
} else { | ||
require.Equal(t, tt.Output, str) | ||
} | ||
|
||
}) | ||
} | ||
} |