Skip to content

Commit

Permalink
improve error messages and added an additional error case
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Dec 12, 2023
1 parent 4ae19c0 commit 68fb1be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
27 changes: 24 additions & 3 deletions internal/resolution/pm/pip/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (j *Job) Run() {
func (j *Job) handleInstallError(cmdErr job.IError) {
var buildError = regexp.MustCompile("setup.py[ install for]*(?P<dependency>[^ ]*) did not run successfully.")
var credentialError = regexp.MustCompile("WARNING: 401 Error, Credentials not correct for")
var couldNotFindVersionError = regexp.MustCompile("Could not find a version that satisfies the requirement")

switch {
case buildError.MatchString(cmdErr.Error()):
Expand Down Expand Up @@ -141,9 +142,29 @@ func (j *Job) handleInstallError(cmdErr job.IError) {
[]string{
"Failed to install python dependency ",
dependencyName,
" due to authorization. This could mean it is",
" a private dependency that the debricked CLI",
" does not have access to.",
" due to authorization.\n" + util.InstalLPrivateDependencyMessage,
}, ""),
)

case couldNotFindVersionError.MatchString(cmdErr.Error()):
// Define a regex to match the dependency name
dependencyNamePattern := regexp.MustCompile(`Could not find a version that satisfies the requirement ([\w=]+)`)
dependencyNameMatch := dependencyNamePattern.FindStringSubmatch(cmdErr.Error())

dependencyName := ""
if len(dependencyNameMatch) > 1 {
// Split the dependency name and version
dependency := strings.Split(dependencyNameMatch[1], "==")
dependencyName = "\"" + dependency[0] + "\""
}

// Set the documentation string
cmdErr.SetDocumentation(
strings.Join(
[]string{
"Failed to find a version that satisfies the requirement for python dependency ",
dependencyName,
". This could mean that the specified version or version does not exist.\n" + util.InstalLPrivateDependencyMessage,
}, ""),
)
}
Expand Down
54 changes: 30 additions & 24 deletions internal/resolution/pm/pip/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,39 @@ func TestRunInstallCmdErr(t *testing.T) {
assert.Len(t, j.Errors().GetAll(), 1)
}

func TestRunInstallCmdBuildErr(t *testing.T) {
cmdErr := errors.New(" python setup.py bdist_wheel did not run successfully. ")
cmdFactoryMock := testdata.NewEchoCmdFactory()
cmdFactoryMock.MakeInstallErr = cmdErr
fileWriterMock := &writerTestdata.FileWriterMock{}
j := NewJob("file", true, cmdFactoryMock, fileWriterMock, pipCleaner{})

go jobTestdata.WaitStatus(j)
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
}
func TestRunInstallCmdErrors(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "Build Error",
err: errors.New(" python setup.py bdist_wheel did not run successfully. "),
},
{
name: "Auth Error",
err: errors.New("WARNING: 401 Error, Credentials not correct for <some-pip-registry>\n" +
"No matching distribution found for some-dependency>=0.1.3\n"),
},
{
name: "Version Error",
err: errors.New("Could not find a version that satisfies the requirement test==123"),
},
}

func TestRunInstallCmdAuthErr(t *testing.T) {
cmdErr := errors.New(
"WARNING: 401 Error, Credentials not correct for <some-pip-registry>\n" +
"No matching distribution found for some-dependency>=0.1.3\n",
)
cmdFactoryMock := testdata.NewEchoCmdFactory()
cmdFactoryMock.MakeInstallErr = cmdErr
fileWriterMock := &writerTestdata.FileWriterMock{}
j := NewJob("file", true, cmdFactoryMock, fileWriterMock, pipCleaner{})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmdFactoryMock := testdata.NewEchoCmdFactory()
cmdFactoryMock.MakeInstallErr = tt.err
fileWriterMock := &writerTestdata.FileWriterMock{}
j := NewJob("file", true, cmdFactoryMock, fileWriterMock, pipCleaner{})

go jobTestdata.WaitStatus(j)
j.Run()
go jobTestdata.WaitStatus(j)
j.Run()

assert.Len(t, j.Errors().GetAll(), 1)
assert.Len(t, j.Errors().GetAll(), 1)
})
}
}

func TestRunInstallCmdOutputErr(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/resolution/pm/util/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type PMJobError struct {
status string
}

var InstalLPrivateDependencyMessage = "If this is a private dependency, please make sure that the debricked CLI has access to install it or pre-install it before running the debricked CLI."

func (e PMJobError) Error() string {
return e.err
}
Expand Down Expand Up @@ -43,7 +45,7 @@ func NewPMJobError(err string) *PMJobError {
return &PMJobError{
err: err,
cmd: "",
doc: "No specific documentation for this problem yet, please report it to us! :)",
doc: "No specific documentation for this problem yet, please create an issue here: https://github.com/debricked/cli/issues",
status: "",
}
}
2 changes: 1 addition & 1 deletion internal/resolution/pm/util/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestPMJobErrorSetDocumentation(t *testing.T) {
jobError := NewPMJobError("")
assert.Equal(
t,
string("No specific documentation for this problem yet, please report it to us! :)\n"),
string("No specific documentation for this problem yet, please create an issue here: https://github.com/debricked/cli/issues\n"),
jobError.Documentation(),
)
jobError.SetDocumentation("documentation")
Expand Down

0 comments on commit 68fb1be

Please sign in to comment.