Skip to content

Fix football module: response body closed, invalid-league crash, unclear errors, and add test coverage - #2133

Merged
Seanstoppable merged 6 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-issue-1874-football-api-calls-is-broken-8d9a88
Jul 26, 2026
Merged

Seanstoppable merged 6 commits into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-issue-1874-football-api-calls-is-broken-8d9a88

Conversation

@Seanstoppable

@Seanstoppable Seanstoppable commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes several bugs in the football module (issue #1874) and significantly improves its test coverage (13.1% → 88.5%).

Bugs fixed

1. "http2: response body closed" on every request (the reported bug)

footballRequest() in modules/football/client.go closed resp.Body via defer before returning the response to its caller:

resp, err := httpClient.Do(req)
...
defer func() { _ = resp.Body.Close() }()
return resp, nil

The deferred close ran when footballRequest returned, so by the time GetStandings/GetMatches in widget.go called io.ReadAll(resp.Body), the body was already closed — producing the "response body closed" error (surfaced as an HTTP/2 stream error) even with a valid API key.

widget.go's callers already correctly defer resp.Body.Close() themselves after checking for errors, so the close in client.go was both wrong and redundant. Removed it.

2. Silent/garbled errors on non-2xx API responses

Added a status-code check in footballRequest so a bad/expired API key, rate limiting, etc. now returns a clear unexpected status code %d from football API error instead of a response whose body may be unusable.

3. Crash on invalid/misspelled league code

NewWidget's error branch (e.g. league set to SA instead of PL) built a Widget without initializing the embedded view.TextWidget, leaving its embedded *view.Base nil. The very first render call needs widget.CommonSettings(), which dereferenced that nil Base and crashed the whole app at startup:

panic: runtime error: invalid memory address or nil pointer dereference
github.com/wtfutil/wtf/view.(*Base).CommonSettings(...)

Fixed by always initializing TextWidget via view.NewTextWidget, even on the invalid-league error path, so the widget renders its error message instead of crashing.

4. Confusing "Error fetching..." message for empty (but successful) results

An empty standings/matches list previously showed a generic Error fetching ... message, which looked like a failure even though the request succeeded and simply had no data for the given range/competition:

  • GetStandings: now shows No standings found for this competition
  • GetMatches: now shows No matches found between <from> and <to>

Test coverage

Added modules/football/client_test.go and modules/football/widget_test.go using httptest fake servers (no real API key needed) covering:

  • Response body remains readable after footballRequest returns (regression test for bug Add Reading List section #1)
  • Non-2xx status handling (regression test for bug JIRA: List of all issues in current sprint assigned to me #2)
  • NewWidget with an invalid league code no longer panics (regression test for bug BambooHR improvements #3)
  • NewWidget/getLeague success paths
  • GetStandings filtering by standingCount, and the new empty-result message
  • GetMatches splitting scheduled/finished matches, favorite-team marking (home/away/none), and the new empty-result message
  • content() happy path

modules/football coverage: 13.1% → 88.5%.

Testing

  • go build ./... ✅
  • go vet ./modules/football/... ✅
  • go test ./modules/football/... ✅ (15 tests, all passing)

Fixes #1874

Seanstoppable and others added 6 commits July 25, 2026 17:19
footballRequest() was closing resp.Body via defer before returning it,
causing callers in widget.go to read an already-closed body and
surface 'http2: response body closed' errors even with a valid API
key. Also add a status code check so non-2xx responses (e.g. bad/
expired key, rate limiting) produce a clear error instead.

Fixes wtfutil#1874

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Cover: response body remains readable after footballRequest returns,
and non-2xx responses surface a clear error.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Previously an empty (but successfully fetched) standings/matches list
showed a generic 'Error fetching ...' message, which was confusing --
it looked like a failure even though the request succeeded and simply
had no data for the given range/competition.

- GetStandings: 'No standings found for this competition'
- GetMatches: 'No matches found between <from> and <to>'

Also add widget_test.go with coverage for GetStandings/GetMatches:
filtering by standingCount, scheduled/finished splitting, favorite
team marking, and the new empty-result messages.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
NewWidget's error branch (invalid/unknown league code, e.g. 'SA'
instead of 'PL') built a Widget without initializing the embedded
view.TextWidget, leaving its embedded *view.Base nil. The very first
render call needs widget.CommonSettings(), which dereferences that nil
Base and crashes the whole app at startup with:

  panic: runtime error: invalid memory address or nil pointer dereference
  github.com/wtfutil/wtf/view.(*Base).CommonSettings(...)

Fix: always initialize TextWidget via view.NewTextWidget in NewWidget,
even on the invalid-league error path, so the widget can still render
its error message instead of crashing.

Add a regression test that constructs NewWidget with an invalid league
code and calls CommonSettings()/content() to confirm it no longer
panics.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Cover getLeague success/failure, markFavorite's home/away/no-favorite
branches, content()'s happy path, and NewWidget's valid-league success
path. Raises modules/football coverage from 13.1%% to 88.5%%.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Seanstoppable Seanstoppable changed the title Fix football module: don't close response body before caller reads it Fix football module: response body closed, invalid-league crash, unclear errors, and add test coverage Jul 26, 2026
@Seanstoppable
Seanstoppable merged commit 5080c38 into wtfutil:trunk Jul 26, 2026
4 checks passed
@Seanstoppable
Seanstoppable deleted the seanstoppable-issue-1874-football-api-calls-is-broken-8d9a88 branch July 26, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Football api calls is broken

1 participant

⌂ 换网址