Fix lint issues: error wrapping, require assertions, formatting

This commit is contained in:
sh9336 2026-01-10 10:26:46 +05:30
parent 4a81d5807a
commit cd9b91bb2e
2 changed files with 8 additions and 6 deletions

View File

@ -51,9 +51,10 @@ func decodeJSON(r io.Reader, obj any) error {
decoder.DisallowUnknownFields() decoder.DisallowUnknownFields()
} }
if err := decoder.Decode(obj); err != nil { 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 fmt.Errorf("empty request body: %w", err)
} }
return err return err
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) { func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
@ -20,7 +21,7 @@ func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
c, _ := gin.CreateTestContext(w) c, _ := gin.CreateTestContext(w)
req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBuffer(nil)) req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBuffer(nil))
assert.NoError(t, err) require.NoError(t, err)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
c.Request = req c.Request = req
@ -28,11 +29,11 @@ func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
var r Req var r Req
err = c.ShouldBindJSON(&r) err = c.ShouldBindJSON(&r)
assert.Error(t, err) require.Error(t, err)
// Current behavior returns plain "EOF", which is not helpful. // Error message should be more descriptive than plain EOF,
assert.NotEqual(t, "EOF", err.Error(), "error message should not be plain EOF") // while still preserving io.EOF via wrapping.
assert.NotEqual(t, "EOF", err.Error())
assert.Contains(t, err.Error(), "empty request body") assert.Contains(t, err.Error(), "empty request body")
assert.ErrorIs(t, err, io.EOF) assert.ErrorIs(t, err, io.EOF)
} }