From c4fb82858e6b2339889f6fdaec9ed4a41ee49cb7 Mon Sep 17 00:00:00 2001 From: Amal Saidov <58995855+amalsaidov@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:10:04 +0000 Subject: [PATCH] enhance(lfs): report status and body on non-JSON LFS error responses (#40331) A push mirror or migration pointed at a remote that has LFS disabled fails with a confusing error like `readObjectStart: expect { or n, but found N ... |Not Found`. That comes from decoding the batch endpoint's plain-text "Not Found" response as JSON. Read the response body before decoding and, when it is not valid LFS JSON, return the HTTP status together with the raw body so the message points at the real cause. Responses that are proper LFS JSON errors behave as before. Fixes: https://github.com/go-gitea/gitea/issues/34897 Assisted-by: Claude Code:claude-opus-4-8 --------- Co-authored-by: amalsaidov Co-authored-by: wxiaoguang --- modules/lfs/http_client.go | 19 ++++++++++--------- modules/lfs/http_client_test.go | 8 ++++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index 4d596293841..e89671636d5 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -17,6 +17,7 @@ import ( "gitea.dev/modules/log" "gitea.dev/modules/proxy" "gitea.dev/modules/setting" + "gitea.dev/modules/util" "golang.org/x/sync/errgroup" ) @@ -273,16 +274,16 @@ func performRequest(ctx context.Context, client *http.Client, req *http.Request) } func handleErrorResponse(resp *http.Response) error { - var er ErrorResponse - err := json.NewDecoder(resp.Body).Decode(&er) + // Non-LFS endpoints might reply with plain text or HTML, read the body ahead to use it as the error message if JSON decoding fails. + // The returned error message might be saved into database's TEXT column, so we also need to truncate it. + body, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024)) if err != nil { - if err == io.EOF { - return io.ErrUnexpectedEOF - } - log.Error("Error decoding json: %v", err) - return err + return fmt.Errorf("LFS server returned error, status: %d, err: %w", resp.StatusCode, err) } - log.Trace("ErrorResponse(%v): %v", resp.Status, er) - return errors.New(er.Message) + var er ErrorResponse + if err := json.Unmarshal(body, &er); err == nil && er.Message != "" { + return fmt.Errorf("LFS server error: %s", util.TruncateRunes(er.Message, 1000)) + } + return fmt.Errorf("LFS server returned error, status: %d, body: %q", resp.StatusCode, util.TruncateRunes(string(body), 1000)) } diff --git a/modules/lfs/http_client_test.go b/modules/lfs/http_client_test.go index c2998d3e272..031b3e8fad6 100644 --- a/modules/lfs/http_client_test.go +++ b/modules/lfs/http_client_test.go @@ -48,6 +48,8 @@ func lfsTestRoundtripHandler(req *http.Request) *http.Response { if strings.Contains(url, "status-not-ok") { return &http.Response{StatusCode: http.StatusBadRequest} + } else if strings.Contains(url, "plaintext-error") { + return &http.Response{StatusCode: http.StatusNotFound, Status: "404 Not Found", Body: io.NopCloser(strings.NewReader("Not Found"))} } else if strings.Contains(url, "invalid-json-response") { return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("invalid json"))} } else if strings.Contains(url, "valid-batch-request-download") { @@ -174,7 +176,8 @@ func TestHTTPClientTransfers(t *testing.T) { { name: "download", cases: []clientCase{ - {"https://status-not-ok.io", io.ErrUnexpectedEOF.Error()}, + {"https://status-not-ok.io", `LFS server returned error, status: 400, body: ""`}, + {"https://plaintext-error.io", `LFS server returned error, status: 404, body: "Not Found"`}, {"https://invalid-json-response.io", "/(invalid json|invalid character)/"}, {"https://valid-batch-request-download.io", ""}, {"https://response-no-objects.io", ""}, @@ -191,7 +194,8 @@ func TestHTTPClientTransfers(t *testing.T) { { name: "upload", cases: []clientCase{ - {"https://status-not-ok.io", io.ErrUnexpectedEOF.Error()}, + {"https://status-not-ok.io", `LFS server returned error, status: 400, body: ""`}, + {"https://plaintext-error.io", `LFS server returned error, status: 404, body: "Not Found"`}, {"https://invalid-json-response.io", "/(invalid json|invalid character)/"}, {"https://valid-batch-request-upload.io", ""}, {"https://response-no-objects.io", ""},