mirror of https://github.com/gin-gonic/gin.git
Adding some error handlers on context and context.go files
This commit is contained in:
parent
1c48977cca
commit
bdaf3f934f
19
context.go
19
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
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue