From 85b9a7254edc6b279fc3d40d13128e49e01a1770 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Fri, 26 Jan 2024 09:22:57 +0330 Subject: [PATCH] fix error handling defer close in repo layer --- repository/mysql/kind_box_req/kind_box_req.go | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 9889051..9dcc0b1 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -4,9 +4,7 @@ import ( "context" "database/sql" "errors" - "git.gocasts.ir/ebhomengo/niki/entity" - "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -29,13 +27,16 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en return kindBoxReq, nil } -func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error { +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) (finalErr error) { op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) + defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { @@ -79,13 +80,15 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) { return true, nil } -func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { +func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) (finalErr error) { op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { return richerror.New(op).WithErr(err). @@ -100,13 +103,15 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description return nil } -func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { +func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) (finalErr error) { op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set status = ? where id = ?`) defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { return richerror.New(op).WithErr(err).