mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
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 <amalsaidov@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
2 files changed
+16
-11
No files matched your search
@@ -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))
|
||||
}
|
||||
@@ -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", ""},
|
||||
|
||||
Reference in new issue
Block a user