Skip to content

Commit

Permalink
Merge pull request #8 from android-project-46group/fix/error-handling
Browse files Browse the repository at this point in the history
エラーハンドリング周りの修正
  • Loading branch information
kokoichi206 authored Sep 10, 2023
2 parents 88b95d0 + 7dd8c55 commit 87d77b3
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Run Test
run: |
Expand Down
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ linters-settings:
settings:
mnd:
ignored-numbers: "0o666,0o777"
funlen:
lines: 77
statements: 50
cyclop:
skip-tests: true
max-complexity: 12
package-average: 0
linters:
enable-all: true
disable:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/android-project-46group/core-api

go 1.19
go 1.21

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ func main() {
log.Fatal("failed to initialize logger: ", err)
}

defer fileCloser()
defer func() {
if err := fileCloser(); err != nil {
logger.Warnf(context.Background(), "failed to fileCloser: ", err)
}
}()

tracer, traceCloser, err := util.NewJaegerTracer(cfg.Service)
if err != nil {
logger.Errorf(context.Background(), "cannot initialize jaeger tracer: ", err)
}

defer traceCloser.Close()
defer func() {
if err := traceCloser.Close(); err != nil {
logger.Warnf(context.Background(), "failed to traceCloser: ", err)
}
}()
opentracing.SetGlobalTracer(tracer)

database, err := db.New(context.Background(), cfg, logger)
Expand Down
7 changes: 6 additions & 1 deletion repository/database/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func (d *database) ListMembers(ctx context.Context) ([]*model.Member, error) {
if err != nil {
return nil, fmt.Errorf("failed to conn.Querytext: %w", err)
}
defer rows.Close()

defer func() {
if err := rows.Close(); err != nil {
d.logger.Warnf(ctx, "failed to rows.Close: ", err)
}
}()

members := []*model.Member{}

Expand Down
10 changes: 4 additions & 6 deletions repository/remote/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import (
"net/http"
)

func (r *remote) GetImage(ctx context.Context, url string) (io.Reader, func(), error) {
func (r *remote) GetImage(ctx context.Context, url string) (io.ReadCloser, error) {
reader := bytes.NewReader([]byte{})

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, reader)
if err != nil {
return nil, nil, fmt.Errorf("failed to NewRequestWithContext: %w", err)
return nil, fmt.Errorf("failed to NewRequestWithContext: %w", err)
}

//nolint:bodyclose
resp, err := r.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("failed to client.Get: %w", err)
return nil, fmt.Errorf("failed to client.Get: %w", err)
}

return resp.Body, func() {
resp.Body.Close()
}, nil
return resp.Body, nil
}
4 changes: 2 additions & 2 deletions repository/reoisitory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ type Database interface {

// HTTP 通信を伴う interface。
type Remote interface {
// 対象 URL の画像を byte 配列で取得する
GetImage(ctx context.Context, url string) (io.Reader, func(), error)
// 対象 URL の画像を取得する
GetImage(ctx context.Context, url string) (io.ReadCloser, error)
}
50 changes: 39 additions & 11 deletions usecase/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -83,12 +84,18 @@ func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) erro
return nil
}

func (u *usecase) writeMembersJSON(members []*model.Member, fullPath string) error {
func (u *usecase) writeMembersJSON(members []*model.Member, fullPath string) (err error) {
file, err := os.Create(fullPath)
if err != nil {
return fmt.Errorf("failed to os.Create: %w", err)
}
defer file.Close()

defer func() {
closeErr := file.Close()
if closeErr != nil {
err = errors.Join(err, fmt.Errorf("failed to close json file: %w", closeErr))
}
}()

bytes, err := json.MarshalIndent(members, "", " ")
if err != nil {
Expand All @@ -103,22 +110,32 @@ func (u *usecase) writeMembersJSON(members []*model.Member, fullPath string) err
return nil
}

func (u *usecase) downloadImage(ctx context.Context, url, fullPath string) error {
reader, closer, err := u.remote.GetImage(ctx, url)
func (u *usecase) downloadImage(ctx context.Context, url, fullPath string) (err error) {
readCloser, err := u.remote.GetImage(ctx, url)
if err != nil {
return fmt.Errorf("failed to GetImage: %w", err)
}

defer closer()
defer func() {
closeErr := readCloser.Close()
if closeErr != nil {
err = errors.Join(err, fmt.Errorf("failed to close readCloser: %w", closeErr))
}
}()

file, err := os.Create(fullPath)
if err != nil {
return fmt.Errorf("failed to os.Create: %w", err)
}

defer file.Close()
defer func() {
closeErr := file.Close()
if closeErr != nil {
err = errors.Join(err, fmt.Errorf("failed to close img file: %w", closeErr))
}
}()

_, err = io.Copy(file, reader)
_, err = io.Copy(file, readCloser)
if err != nil {
return fmt.Errorf("failed to io.Copy: %w", err)
}
Expand Down Expand Up @@ -155,12 +172,17 @@ func (u *usecase) initializeImgDir(imgDir string) error {
}

// targetDir 配下のファイルを再帰的に zip 化し、渡された writer に書き込む。
func (u *usecase) createZip(targetDir string, readWriter io.Writer) error {
func (u *usecase) createZip(targetDir string, readWriter io.Writer) (err error) {
zipWriter := zip.NewWriter(readWriter)
defer zipWriter.Close()
defer func() {
closeErr := zipWriter.Close()
if closeErr != nil {
err = errors.Join(err, fmt.Errorf("failed to close img file: %w", closeErr))
}
}()

// ディレクトリを再帰的に探索する。
err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error {
err = filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("failed to Walk: %w", err)
}
Expand All @@ -182,7 +204,13 @@ func (u *usecase) createZip(targetDir string, readWriter io.Writer) error {
if err != nil {
return fmt.Errorf("failed to Open: %w", err)
}
defer fileToZip.Close()

defer func() {
closeErr := fileToZip.Close()
if closeErr != nil {
err = errors.Join(err, closeErr)
}
}()

fileToZipStat, err := fileToZip.Stat()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions usecase/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func TestDownloadMembersZip(t *testing.T) {
},
},
remote: &mockRemote{
GetImageFunc: func(ctx context.Context, url string) (io.Reader, func(), error) {
GetImageFunc: func(ctx context.Context, url string) (io.ReadCloser, error) {
data := imgBinary

reader := bytes.NewReader(data)

return reader, func() {}, nil
return io.NopCloser(reader), nil
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions usecase/mock_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func (m *mockDatabase) ListMembers(ctx context.Context) ([]*model.Member, error)
}

type mockRemote struct {
GetImageFunc func(ctx context.Context, url string) (io.Reader, func(), error)
GetImageFunc func(ctx context.Context, url string) (io.ReadCloser, error)
}

func (m *mockRemote) GetImage(ctx context.Context, url string) (io.Reader, func(), error) {
func (m *mockRemote) GetImage(ctx context.Context, url string) (io.ReadCloser, error) {
return m.GetImageFunc(ctx, url)
}
7 changes: 4 additions & 3 deletions util/logger/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewFileLogger(
path string,
host string,
service string,
) (Logger, func(), error) {
) (Logger, func() error, error) {
//nolint:nosnakecase
logfile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o666)
if err != nil {
Expand All @@ -40,8 +40,9 @@ func NewFileLogger(
service: service,
}

return logger, func() {
logfile.Close()
return logger, func() error {
//nolint:wrapcheck
return logfile.Close()
}, nil
}

Expand Down

0 comments on commit 87d77b3

Please sign in to comment.