Skip to content

Commit

Permalink
#71: Fix deferred error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Apr 8, 2024
1 parent 4557d4e commit 6dc4e20
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion files.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ func FilePutContents(fileName, data string, flags ...interface{}) (int, error) {
}

f, err := os.OpenFile(fileName, v|os.O_WRONLY, 0644)
defer f.Close()
defer func(f *os.File) {
err = f.Close()
if err != nil {
fmt.Println(err)
}
}(f)

if err != nil {
return -1, err
Expand Down
23 changes: 20 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
Expand All @@ -27,7 +28,12 @@ func (c *Context) doRequest(path string) (string, error) {
return "", err
}

defer resp.Body.Close()
defer func(body io.ReadCloser) {
err = body.Close()
if err != nil {
fmt.Println(err)

Check warning on line 34 in request.go

View check run for this annotation

Codecov / codecov/patch

request.go#L34

Added line #L34 was not covered by tests
}
}(resp.Body)

content, cErr := io.ReadAll(resp.Body)

Expand Down Expand Up @@ -56,15 +62,26 @@ func (c *Context) uploadFile(fieldName, filePath string) bool {
return false
}

defer file.Close()
defer func(file multipart.File) {
err = file.Close()
if err != nil {
fmt.Println(err)

Check warning on line 68 in request.go

View check run for this annotation

Codecov / codecov/patch

request.go#L65-L68

Added lines #L65 - L68 were not covered by tests
}
}(file)

f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return false
}

defer f.Close()
defer func(f *os.File) {
err = f.Close()
if err != nil {
fmt.Println(err)

Check warning on line 81 in request.go

View check run for this annotation

Codecov / codecov/patch

request.go#L78-L81

Added lines #L78 - L81 were not covered by tests
}
}(f)

_, err = io.Copy(f, file)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 6dc4e20

Please sign in to comment.