Skip to content

Commit

Permalink
Improve resolution error output to include current status
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed Oct 13, 2023
1 parent 661a7b8 commit d519d24
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
31 changes: 25 additions & 6 deletions internal/resolution/job/base_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
}
}

Expand All @@ -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

Check failure on line 48 in internal/resolution/job/base_job.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)

}

func (j *BaseJob) SendStatus(status string) {
j.currentStatus = status
j.status <- status
}

Expand Down
24 changes: 24 additions & 0 deletions internal/resolution/job/base_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job

import (
"errors"
"fmt"
"os/exec"
"testing"

Expand Down Expand Up @@ -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{}
Expand Down
12 changes: 6 additions & 6 deletions internal/resolution/pm/pip/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ func (j *Job) Install() bool {
func (j *Job) Run() {
if j.install {
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
}

j.SendStatus("installing requirements")
_, err = j.runInstallCmd()
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(j.FmtError(err, nil))

return
}
}

err := j.writeLockContent()
if err != nil {
j.Errors().Critical(err)
j.Errors().Critical(j.FmtError(err, nil))

return
}
Expand All @@ -88,7 +88,7 @@ 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))
}
}
}
Expand Down Expand Up @@ -143,7 +143,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
Expand Down

0 comments on commit d519d24

Please sign in to comment.