Adding some error handlers on context and context.go files

This commit is contained in:
vyom 2022-08-20 22:04:48 -04:00
parent 1c48977cca
commit bdaf3f934f
2 changed files with 38 additions and 8 deletions

View File

@ -583,7 +583,10 @@ func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
f.Close() err = f.Close()
if err != nil {
return nil, err
}
return fh, err return fh, err
} }
@ -599,13 +602,23 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
if err != nil { if err != nil {
return err return err
} }
defer src.Close() defer func(src multipart.File) {
err = src.Close()
if err != nil {
}
}(src)
out, err := os.Create(dst) out, err := os.Create(dst)
if err != nil { if err != nil {
return err return err
} }
defer out.Close() defer func(out *os.File) {
err = out.Close()
if err != nil {
}
}(out)
_, err = io.Copy(out, src) _, err = io.Copy(out, src)
return err return err

View File

@ -42,7 +42,12 @@ func createMultipartRequest() *http.Request {
boundary := "--testboundary" boundary := "--testboundary"
body := new(bytes.Buffer) body := new(bytes.Buffer)
mw := multipart.NewWriter(body) 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.SetBoundary(boundary))
must(mw.WriteField("foo", "bar")) must(mw.WriteField("foo", "bar"))
@ -76,7 +81,10 @@ func TestContextFormFile(t *testing.T) {
_, err = w.Write([]byte("test")) _, err = w.Write([]byte("test"))
assert.NoError(t, err) assert.NoError(t, err)
} }
mw.Close() err = mw.Close()
if err != nil {
assert.Error(t, err)
}
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf) c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType()) c.Request.Header.Set("Content-Type", mw.FormDataContentType())
@ -97,7 +105,10 @@ func TestContextMultipartForm(t *testing.T) {
_, err = w.Write([]byte("test")) _, err = w.Write([]byte("test"))
assert.NoError(t, err) assert.NoError(t, err)
} }
mw.Close() err = mw.Close()
if err != nil {
assert.Error(t, err)
}
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf) c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType()) c.Request.Header.Set("Content-Type", mw.FormDataContentType())
@ -112,7 +123,10 @@ func TestContextMultipartForm(t *testing.T) {
func TestSaveUploadedOpenFailed(t *testing.T) { func TestSaveUploadedOpenFailed(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf) mw := multipart.NewWriter(buf)
mw.Close() err := mw.Close()
if err != nil {
assert.Error(t, err)
}
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf) c.Request, _ = http.NewRequest("POST", "/", buf)
@ -132,7 +146,10 @@ func TestSaveUploadedCreateFailed(t *testing.T) {
_, err = w.Write([]byte("test")) _, err = w.Write([]byte("test"))
assert.NoError(t, err) assert.NoError(t, err)
} }
mw.Close() err = mw.Close()
if err != nil {
assert.Error(t, err)
}
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf) c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType()) c.Request.Header.Set("Content-Type", mw.FormDataContentType())