Skip to content

Commit

Permalink
Fix NextJob panic (#411)
Browse files Browse the repository at this point in the history
* Check for nil job
* Write error header
* Add case for NextJob returning nil
  • Loading branch information
stephen-soltesz authored Oct 10, 2022
1 parent fb84887 commit 9043585
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tracker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (h *Handler) nextJob(resp http.ResponseWriter, req *http.Request) *JobWithT
return nil
}
jt := h.jobservice.NextJob(req.Context())
if jt == nil {
resp.WriteHeader(http.StatusInternalServerError)
return nil
}

// Check for empty job (no job found with files)
if jt.Job.Date.Equal(time.Time{}) {
Expand Down
7 changes: 7 additions & 0 deletions tracker/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type fakeJobService struct {
}

func (f *fakeJobService) NextJob(ctx context.Context) *tracker.JobWithTarget {
if f.calls >= len(f.jobs) {
// Return nil if there are no more jobs.
return nil
}
j := f.jobs[f.calls]
f.calls++
return &tracker.JobWithTarget{Job: j}
Expand Down Expand Up @@ -280,4 +284,7 @@ func TestNextJobV2Handler(t *testing.T) {

// This one should fail because the fakeJobService returns a duplicate job.
postAndExpect(t, &url, http.StatusInternalServerError)

// Get nil result.
postAndExpect(t, &url, http.StatusInternalServerError)
}

0 comments on commit 9043585

Please sign in to comment.