Include request context in "parsing failed" errors

What does this MR do and why?

When the internal API returns a non-JSON body, gitlabnet.ParseJSON reported only "parsing failed", with no indication of which request produced the response. As reported in #888, during LFS-over-SSH setup this makes the failure impossible to diagnose from gitlab-shell logs alone: every internal-API call looks identical in the logs, so there is no way to tell which endpoint returned the non-JSON response.

This MR makes the failure diagnosable from the logs: when JSON decoding fails, ParseJSON now logs the request URL, the status code, the response content type and the decoding error (as LabKit fields, using the request's context) at the point of failure.

The returned error stays exactly "parsing failed". An earlier version of this MR put the context into the error string itself; review (thank you, GitLab Duo) pointed out that cmd/gitlab-shell, the sshd session and the 2FA / personal access token commands print the error to the SSH client's stderr, which would expose the internal API URL and — via the wrapped decoding error — internal Go struct field names. The context therefore belongs in the server-side log, not in the client-visible error. The transport already logs http_url and status per request; the new log line adds the content type and the decoding error at the exact call that failed.

This is a diagnostics-only change: no response handling, status-code logic, or retry behaviour is modified, and the underlying non-JSON response itself (server-side, unknown) is out of scope.

Root cause

internal/gitlabnet/client.go, ParseJSON: the function receives the full *http.Response — which carries Request.URL, StatusCode and Header — but on a decode error it returned a bare fmt.Errorf("parsing failed"), dropping all of that context as well as the decode error itself. ParseJSON is the shared decoder for the gitlabnet sub-clients (accessverifier, authorizedcerts/keys, discover, healthcheck, lfsauthenticate, lfstransfer, personalaccesstoken, twofactorrecover/verify), so fixing it at this layer gives every internal-API call diagnosable failures, including the LFS batch/authenticate calls named in #888.

Note: the newer internal/clients/gitlab package has the same pattern (errors.New("parsing failed") in its ParseJSON). I left it untouched to keep this MR scoped to the reported LFS path; happy to extend it in this MR or a follow-up if you'd prefer consistency.

Test plan

New frozen-gate regression test TestParseJSONErrorStaysGenericAndLogsContext in internal/gitlabnet/client_test.go: serves a non-JSON 200 text/html response from an httptest server and asserts (a) the returned error is exactly "parsing failed" and (b) one error-level log record carries the request URL, the status code and the content type. It fails on the previous implementation (which returned the context inside the error and logged nothing) and passes with this change.

No existing test needed changes in the final version: the returned error string is unchanged, so every exact assertion on "parsing failed" stands as it was.

go test ./internal/gitlabnet/... ./internal/command/...: all 31 packages pass. go test ./...: all packages pass except cmd/gitlab-sshd, whose acceptance test requires a compiled gitlab-sshd binary (make compile) — a pre-existing environment limitation that fails identically on unmodified main. gofmt and go vet clean on all changed files.

Closes #888

Edited by Koussay Mehdouani

Merge request reports

Loading
Loading