fix error handling defer close in repo layer

This commit is contained in:
Abolfazl Nourzad 2024-01-26 09:22:57 +03:30
parent 6853ddaf64
commit 85b9a7254e
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
1 changed files with 16 additions and 11 deletions

View File

@ -4,9 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/logger"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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 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") op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
statement, err := d.conn.Conn(). statement, err := d.conn.Conn().
Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`)
defer func() { defer func() {
dErr := statement.Close() cErr := statement.Close()
logger.L().Error(dErr.Error()) if cErr != nil {
finalErr = cErr
}
}() }()
if err != nil { if err != nil {
@ -79,13 +80,15 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) {
return true, nil 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") op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
statement, err := d.conn.Conn(). statement, err := d.conn.Conn().
Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`)
defer func() { defer func() {
dErr := statement.Close() cErr := statement.Close()
logger.L().Error(dErr.Error()) if cErr != nil {
finalErr = cErr
}
}() }()
if err != nil { if err != nil {
return richerror.New(op).WithErr(err). return richerror.New(op).WithErr(err).
@ -100,13 +103,15 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description
return nil 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") op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus")
statement, err := d.conn.Conn(). statement, err := d.conn.Conn().
Prepare(`update kind_box_reqs set status = ? where id = ?`) Prepare(`update kind_box_reqs set status = ? where id = ?`)
defer func() { defer func() {
dErr := statement.Close() cErr := statement.Close()
logger.L().Error(dErr.Error()) if cErr != nil {
finalErr = cErr
}
}() }()
if err != nil { if err != nil {
return richerror.New(op).WithErr(err). return richerror.New(op).WithErr(err).