mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-12 01:26:56 +08:00
Fix lint issues: error wrapping, require assertions, formatting
This commit is contained in:
parent
4a81d5807a
commit
cd9b91bb2e
@ -51,9 +51,10 @@ func decodeJSON(r io.Reader, obj any) error {
|
||||
decoder.DisallowUnknownFields()
|
||||
}
|
||||
if err := decoder.Decode(obj); err != nil {
|
||||
if err == io.EOF {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return fmt.Errorf("empty request body: %w", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
|
||||
@ -20,7 +21,7 @@ func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBuffer(nil))
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
c.Request = req
|
||||
@ -28,11 +29,11 @@ func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
|
||||
var r Req
|
||||
err = c.ShouldBindJSON(&r)
|
||||
|
||||
assert.Error(t, err)
|
||||
require.Error(t, err)
|
||||
|
||||
// Current behavior returns plain "EOF", which is not helpful.
|
||||
assert.NotEqual(t, "EOF", err.Error(), "error message should not be plain EOF")
|
||||
// Error message should be more descriptive than plain EOF,
|
||||
// while still preserving io.EOF via wrapping.
|
||||
assert.NotEqual(t, "EOF", err.Error())
|
||||
assert.Contains(t, err.Error(), "empty request body")
|
||||
assert.ErrorIs(t, err, io.EOF)
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user