diff --git a/internal/resolution/job/base_job.go b/internal/resolution/job/base_job.go index df07acc4..d83503fe 100644 --- a/internal/resolution/job/base_job.go +++ b/internal/resolution/job/base_job.go @@ -2,20 +2,23 @@ package job import ( "errors" + "fmt" "os/exec" ) type BaseJob struct { - file string - errs IErrors - status chan string + file string + errs IErrors + status chan string + currentStatus string } func NewBaseJob(file string) BaseJob { return BaseJob{ - file: file, - errs: NewErrors(file), - status: make(chan string), + file: file, + errs: NewErrors(file), + status: make(chan string), + currentStatus: "", } } @@ -31,7 +34,23 @@ func (j *BaseJob) ReceiveStatus() chan string { return j.status } +func (j *BaseJob) FmtError(err error, output []byte) error { + + if err == nil { + return fmt.Errorf("%s error: No error was present.", j.currentStatus) + } + + errorString := fmt.Errorf("%s error: %s", j.currentStatus, err) + + if output != nil { + errorString = fmt.Errorf("%s output: %s\n%s", j.currentStatus, output, errorString) + } + + return errorString +} + func (j *BaseJob) SendStatus(status string) { + j.currentStatus = status j.status <- status } diff --git a/internal/resolution/job/base_job_test.go b/internal/resolution/job/base_job_test.go index 3512a749..0f4e8024 100644 --- a/internal/resolution/job/base_job_test.go +++ b/internal/resolution/job/base_job_test.go @@ -2,6 +2,7 @@ package job import ( "errors" + "fmt" "os/exec" "testing" @@ -34,6 +35,29 @@ func TestReceiveStatus(t *testing.T) { assert.NotNil(t, statusChan) } +func TestFmtError(t *testing.T) { + j := BaseJob{ + file: testFile, + errs: nil, + status: make(chan string), + } + statusMsg := "statusMsg" + go func() { + status := <-j.ReceiveStatus() + assert.Equal(t, "statusMsg", status) + }() + + j.SendStatus(statusMsg) + + errorMsg := fmt.Errorf("statusMsg error: test-error") + formattedError := j.FmtError(fmt.Errorf("test-error"), nil) + assert.Equal(t, errorMsg, formattedError) + + errorMsg = fmt.Errorf("statusMsg output: test\nstatusMsg error: test-error") + formattedError = j.FmtError(fmt.Errorf("test-error"), []byte("test")) + assert.Equal(t, errorMsg, formattedError) +} + func TestErrors(t *testing.T) { jobErr := errors.New("error") j := BaseJob{} diff --git a/internal/resolution/pm/pip/job.go b/internal/resolution/pm/pip/job.go index 46e51b0e..46cbe078 100644 --- a/internal/resolution/pm/pip/job.go +++ b/internal/resolution/pm/pip/job.go @@ -67,14 +67,14 @@ func (j *Job) Run() { j.SendStatus("removing venv") err := j.pipCleaner.RemoveAll(j.venvPath) if err != nil { - j.Errors().Critical(err) + j.Errors().Critical(j.FmtError(err, nil)) } }() j.SendStatus("creating venv") - _, err := j.runCreateVenvCmd() + output, err := j.runCreateVenvCmd() if err != nil { - j.Errors().Critical(err) + j.Errors().Critical(j.FmtError(err, output)) return } @@ -82,7 +82,7 @@ func (j *Job) Run() { j.SendStatus("installing requirements") _, err = j.runInstallCmd() if err != nil { - j.Errors().Critical(err) + j.Errors().Critical(j.FmtError(err, nil)) return } @@ -90,7 +90,7 @@ func (j *Job) Run() { err := j.writeLockContent() if err != nil { - j.Errors().Critical(err) + j.Errors().Critical(j.FmtError(err, nil)) return } @@ -147,7 +147,7 @@ func (j *Job) runCreateVenvCmd() ([]byte, error) { createVenvCmdOutput, err := createVenvCmd.Output() if err != nil { - return nil, j.GetExitError(err) + return createVenvCmdOutput, j.GetExitError(err) } return createVenvCmdOutput, nil