refactor: Use `errors` package to compare and assert error types by zyfy29 · Pull Request #3739 · google/go-github

itsmylife added a commit to grafana/github-datasource that referenced this pull request

@itsmylife

…ng (#629)

## Summary

Fixes a nil pointer dereference panic caused by the interaction between
go-github v81's
`ErrorResponse.Is` and the SDK's `guessErrorStatus` function.

go-github v76 (bumped from v72 to v81 in #574) changed
`ErrorResponse.Is` from a simple type assertion
to calling `errors.As(target, &v)` on the target. See PR:
google/go-github#3739
The SDK's `guessErrorStatus`
(`backend/status.go:112`) passes typed nil targets (`(*url.Error)(nil)`)
to `errors.Is`.
When `errors.Is` walks the error chain and reaches
`*github.ErrorResponse`, the v81
`ErrorResponse.Is` calls `errors.As` on that typed nil, which tries to
call `Unwrap()` on
the nil receiver — causing a nil pointer dereference panic.

The fix adds `sanitizeGitHubError` which converts
`*github.ErrorResponse` to a plain error
at the `addErrorSourceToError` boundary, preserving the error message
while stripping the
problematic `Is` method from the error chain.

## Manual reproduction steps

1. Run Grafana with the github-datasource plugin (v2.5.0)
2. Configure the GitHub datasource with a valid personal access token
3. Open Explore (or create a panel) and select the GitHub datasource
4. Set query type to **Workflows** (or **Code Scanning**)
5. Set **Owner** to `grafana`, **Repository** to
`this-repo-does-not-exist`
6. Run the query
7. Observe the panic in Grafana server logs:
`panic triggered error="runtime error: invalid memory address or nil
pointer dereference"`

With this fix applied, the query returns a proper error response instead
of panicking.

Fixes grafana/oss-big-tent-squad#183