chore(bind): return 413 status code when error is http.MaxBytesError (#4227)

* Bind: return 413 status code when error is `http.MaxBytesError`

The Go standard library includes a method `http.MaxBytesReader` that allows limiting the request body. For example, users can create a middleware like:

```go
func MiddlewareMaxBodySize(c *gin.Context) {
	// Limit request body to 100 bytes
	c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 100)
	c.Next()
}
```

When the body exceeds the limit, reading from the request body returns an error of type `http.MaxBytesError`.

This PR makes sure that when the error is of kind `http.MaxBytesError`, Gin returns the correct status code 413 (Request Entity Too Large) instead of a generic 400 (Bad Request).

* Disable test when using sonic

* Fix

* Disable for go-json too

* Add references to GitHub issues

* Test that the response is 400 for sonic and go-json
This commit is contained in:
Alessandro (Ale) Segala
2025-05-25 05:36:33 -07:00
committed by GitHub
parent c4287b1300
commit 40725d85ba
6 changed files with 80 additions and 24 deletions
+3
View File
@@ -20,3 +20,6 @@ var (
// NewEncoder is exported by gin/json package.
NewEncoder = json.NewEncoder
)
// Package indicates what library is being used for JSON encoding.
const Package = "github.com/goccy/go-json"
+3
View File
@@ -20,3 +20,6 @@ var (
// NewEncoder is exported by gin/json package.
NewEncoder = json.NewEncoder
)
// Package indicates what library is being used for JSON encoding.
const Package = "encoding/json"
+3
View File
@@ -21,3 +21,6 @@ var (
// NewEncoder is exported by gin/json package.
NewEncoder = json.NewEncoder
)
// Package indicates what library is being used for JSON encoding.
const Package = "github.com/json-iterator/go"
+3
View File
@@ -21,3 +21,6 @@ var (
// NewEncoder is exported by gin/json package.
NewEncoder = json.NewEncoder
)
// Package indicates what library is being used for JSON encoding.
const Package = "github.com/bytedance/sonic"