diff --git a/context.go b/context.go index f9489a77..9d14a96a 100644 --- a/context.go +++ b/context.go @@ -583,7 +583,10 @@ func (c *Context) FormFile(name string) (*multipart.FileHeader, error) { if err != nil { return nil, err } - f.Close() + err = f.Close() + if err != nil { + return nil, err + } return fh, err } @@ -599,13 +602,23 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error if err != nil { return err } - defer src.Close() + defer func(src multipart.File) { + err = src.Close() + if err != nil { + + } + }(src) out, err := os.Create(dst) if err != nil { return err } - defer out.Close() + defer func(out *os.File) { + err = out.Close() + if err != nil { + + } + }(out) _, err = io.Copy(out, src) return err diff --git a/context_test.go b/context_test.go index b3e81c14..1cc016d3 100644 --- a/context_test.go +++ b/context_test.go @@ -42,7 +42,12 @@ func createMultipartRequest() *http.Request { boundary := "--testboundary" body := new(bytes.Buffer) mw := multipart.NewWriter(body) - defer mw.Close() + defer func(mw *multipart.Writer) { + err := mw.Close() + if err != nil { + must(err) + } + }(mw) must(mw.SetBoundary(boundary)) must(mw.WriteField("foo", "bar")) @@ -76,7 +81,10 @@ func TestContextFormFile(t *testing.T) { _, err = w.Write([]byte("test")) assert.NoError(t, err) } - mw.Close() + err = mw.Close() + if err != nil { + assert.Error(t, err) + } c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("POST", "/", buf) c.Request.Header.Set("Content-Type", mw.FormDataContentType()) @@ -97,7 +105,10 @@ func TestContextMultipartForm(t *testing.T) { _, err = w.Write([]byte("test")) assert.NoError(t, err) } - mw.Close() + err = mw.Close() + if err != nil { + assert.Error(t, err) + } c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("POST", "/", buf) c.Request.Header.Set("Content-Type", mw.FormDataContentType()) @@ -112,7 +123,10 @@ func TestContextMultipartForm(t *testing.T) { func TestSaveUploadedOpenFailed(t *testing.T) { buf := new(bytes.Buffer) mw := multipart.NewWriter(buf) - mw.Close() + err := mw.Close() + if err != nil { + assert.Error(t, err) + } c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("POST", "/", buf) @@ -132,7 +146,10 @@ func TestSaveUploadedCreateFailed(t *testing.T) { _, err = w.Write([]byte("test")) assert.NoError(t, err) } - mw.Close() + err = mw.Close() + if err != nil { + assert.Error(t, err) + } c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("POST", "/", buf) c.Request.Header.Set("Content-Type", mw.FormDataContentType())