From 03882e70fc2fb6e2332870d307e900d6d7113ad6 Mon Sep 17 00:00:00 2001 From: litsynp Date: Sun, 22 Dec 2024 12:04:31 +0900 Subject: [PATCH] chore: handle Postgres error from error middleware --- api/errors.go | 4 +- cmd/import_breeds/main.go | 5 +- cmd/server/router.go | 6 ++ internal/common/null.go | 4 +- internal/infra/database/transaction.go | 12 +--- internal/service/breed_service.go | 3 +- internal/service/chat_service.go | 66 +++++++++--------- internal/service/media_service.go | 7 +- internal/service/sos_condition_service.go | 5 +- internal/service/sos_post_service.go | 83 ++++++++++------------- internal/service/user_service.go | 44 ++++++------ 11 files changed, 110 insertions(+), 129 deletions(-) diff --git a/api/errors.go b/api/errors.go index 70bc931..ca8151e 100644 --- a/api/errors.go +++ b/api/errors.go @@ -129,7 +129,9 @@ func FromPostgresError(err error) *AppError { return NewAppError(err, http.StatusBadRequest, ErrCodeBadRequest, "잘못된 값입니다") case strings.Contains(errStr, "violates unique constraint"): return NewAppError(err, http.StatusConflict, ErrCodeConflict, "중복된 값입니다") - default: + case strings.Contains(errStr, "pq:"): return NewAppError(err, http.StatusInternalServerError, ErrCodeUnknown, "알 수 없는 오류가 발생했습니다") + default: + return nil } } diff --git a/cmd/import_breeds/main.go b/cmd/import_breeds/main.go index 3d2a6e1..afa055e 100644 --- a/cmd/import_breeds/main.go +++ b/cmd/import_breeds/main.go @@ -15,7 +15,6 @@ import ( "github.com/pet-sitter/pets-next-door-api/cmd/import_breeds/breedsimporterservice" - pnd "github.com/pet-sitter/pets-next-door-api/api" "github.com/pet-sitter/pets-next-door-api/internal/configs" "github.com/pet-sitter/pets-next-door-api/internal/infra/database" ) @@ -103,7 +102,7 @@ func importBreed( Name: utils.StrToNullStr(row.Breed), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if len(existingList) > 1 { @@ -123,7 +122,7 @@ func importBreed( PetType: petType.String(), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } log.Printf( diff --git a/cmd/server/router.go b/cmd/server/router.go index 383bec9..68969ae 100644 --- a/cmd/server/router.go +++ b/cmd/server/router.go @@ -103,6 +103,12 @@ func NewRouter(app *firebaseinfra.FirebaseApp) (*echo.Echo, error) { return c.JSON(appErr.StatusCode, appErr) } + if err := pnd.FromPostgresError(err); err != nil { + if errors.As(err, &appErr) { + return c.JSON(appErr.StatusCode, appErr) + } + } + return c.JSON(http.StatusInternalServerError, pnd.ErrUnknown(err)) } diff --git a/internal/common/null.go b/internal/common/null.go index 4c597d1..a44c17d 100644 --- a/internal/common/null.go +++ b/internal/common/null.go @@ -3,8 +3,6 @@ package utils import ( "database/sql" "time" - - pnd "github.com/pet-sitter/pets-next-door-api/api" ) func DerefOrEmpty[T any](val *T) T { @@ -106,7 +104,7 @@ func StrToNullTime(val string) (sql.NullTime, error) { if err != nil { return sql.NullTime{ Valid: false, - }, pnd.FromPostgresError(err) + }, err } return sql.NullTime{ Time: parsedTime, diff --git a/internal/infra/database/transaction.go b/internal/infra/database/transaction.go index 4ad299c..c8ca85c 100644 --- a/internal/infra/database/transaction.go +++ b/internal/infra/database/transaction.go @@ -5,8 +5,6 @@ import ( "database/sql" "errors" - pnd "github.com/pet-sitter/pets-next-door-api/api" - "github.com/rs/zerolog/log" ) @@ -20,7 +18,7 @@ type Transactioner interface { func (db *DB) BeginTx(ctx context.Context) (*Tx, error) { tx, err := db.DB.BeginTx(ctx, nil) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return &Tx{tx}, nil @@ -54,18 +52,14 @@ func (tx *Tx) Rollback() error { if errors.Is(err, sql.ErrTxDone) { return nil } - return pnd.FromPostgresError(err) + return err } return nil } func (tx *Tx) Commit() error { - if err := tx.Tx.Commit(); err != nil { - return pnd.FromPostgresError(err) - } - - return nil + return tx.Tx.Commit() } func WithTransaction(ctx context.Context, conn *DB, f func(tx *Tx) error) error { diff --git a/internal/service/breed_service.go b/internal/service/breed_service.go index 918920d..24183ea 100644 --- a/internal/service/breed_service.go +++ b/internal/service/breed_service.go @@ -5,7 +5,6 @@ import ( databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen" - pnd "github.com/pet-sitter/pets-next-door-api/api" "github.com/pet-sitter/pets-next-door-api/internal/domain/breed" "github.com/pet-sitter/pets-next-door-api/internal/infra/database" ) @@ -25,7 +24,7 @@ func (s *BreedService) FindBreeds( ) (*breed.ListView, error) { rows, err := databasegen.New(s.conn).FindBreeds(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return breed.ToListViewFromRows(params.Page, params.Size, rows), nil diff --git a/internal/service/chat_service.go b/internal/service/chat_service.go index f28e0b7..b960417 100644 --- a/internal/service/chat_service.go +++ b/internal/service/chat_service.go @@ -33,7 +33,7 @@ func (s *ChatService) CreateRoom( FbUid: utils.StrToNullStr(userFirebaseUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // 채팅방 생성 @@ -50,14 +50,13 @@ func (s *ChatService) CreateRoom( } q := databasegen.New(tx) - row, databaseGenError := q.CreateRoom(ctx, databasegen.CreateRoomParams{ + row, err := q.CreateRoom(ctx, databasegen.CreateRoomParams{ ID: chatRoomUUID, Name: name, RoomType: roomType, }) - - if databaseGenError != nil { - return nil, pnd.FromPostgresError(databaseGenError) + if err != nil { + return nil, err } joinRoomUUID, joinRoomUUIDError := uuid.NewV7() @@ -71,7 +70,7 @@ func (s *ChatService) CreateRoom( RoomID: row.ID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -90,13 +89,12 @@ func (s *ChatService) JoinRoom( FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // 채팅방이 현재 존재하는지 확인 - existRoom, existRoomError := databasegen.New(s.conn).ExistsRoom(ctx, roomID) - - if existRoomError != nil { - return nil, pnd.FromPostgresError(existRoomError) + existRoom, err := databasegen.New(s.conn).ExistsRoom(ctx, roomID) + if err != nil { + return nil, err } if !existRoom { @@ -106,7 +104,7 @@ func (s *ChatService) JoinRoom( // 채팅방에 이미 참여중인지 확인 existsUser, existsUserError := databasegen.New(s.conn).UserExistsInRoom(ctx, roomID) if existsUserError != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // 채팅방에 참여하지 않은 경우 참여 @@ -123,7 +121,7 @@ func (s *ChatService) JoinRoom( UserID: userData.ID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return chat.ToJoinRoom(row), nil @@ -137,7 +135,7 @@ func (s *ChatService) LeaveRoom(ctx context.Context, roomID uuid.UUID, fbUID str FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return pnd.FromPostgresError(err) + return err } err = databasegen.New(s.conn).LeaveRoom(ctx, databasegen.LeaveRoomParams{ @@ -145,17 +143,17 @@ func (s *ChatService) LeaveRoom(ctx context.Context, roomID uuid.UUID, fbUID str UserID: userData.ID, }) if err != nil { - return pnd.FromPostgresError(err) + return err } exists, err := databasegen.New(s.conn).UserExistsInRoom(ctx, roomID) if err != nil { - return pnd.FromPostgresError(err) + return err } if !exists { err = databasegen.New(s.conn).DeleteRoom(ctx, roomID) if err != nil { - return pnd.FromPostgresError(err) + return err } } return nil @@ -169,11 +167,11 @@ func (s *ChatService) FindAllByUserUID( FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } rows, err := databasegen.New(s.conn).FindAllUserChatRoomsByUserUID(ctx, userData.ID) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // rows를 반복하며 각 row에 대해 ToJoinRoom을 호출하여 JoinRoom으로 변환 @@ -191,7 +189,7 @@ func (s *ChatService) FindChatRoomByUIDAndRoomID( FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } row, err := databasegen.New(s.conn).FindRoomByIDAndUserID( @@ -199,7 +197,7 @@ func (s *ChatService) FindChatRoomByUIDAndRoomID( databasegen.FindRoomByIDAndUserIDParams{ID: roomID, UserID: userData.ID}, ) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return chat.ToUserChatRoomView(row), nil @@ -224,7 +222,7 @@ func (s *ChatService) FindChatRoomMessagesByRoomID( RoomID: roomID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // rows의 맨 앞의 ID값을 가져온다. @@ -239,24 +237,22 @@ func (s *ChatService) FindChatRoomMessagesByRoomID( // 가장 오래된 메시지의 ID를 가져온다. lastID := rows[len(rows)-1].ID - hasPrev, hasPrevError := databasegen.New(s.conn). + hasPrev, err := databasegen.New(s.conn). HasPrevMessages(ctx, databasegen.HasPrevMessagesParams{ ID: lastID, RoomID: roomID, }) - - if hasPrevError != nil { - return nil, pnd.FromPostgresError(hasPrevError) + if err != nil { + return nil, err } - hasNext, hasNextError := databasegen.New(s.conn). + hasNext, err := databasegen.New(s.conn). HasNextMessages(ctx, databasegen.HasNextMessagesParams{ ID: firstID, RoomID: roomID, }) - - if hasNextError != nil { - return nil, pnd.FromPostgresError(hasNextError) + if err != nil { + return nil, err } return chat.ToUserChatRoomMessageBetweenView(rows, hasNext, hasPrev, &firstID, &lastID), nil @@ -271,7 +267,7 @@ func (s *ChatService) FindChatRoomMessagesByRoomID( RoomID: roomID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if len(rows) == 0 { @@ -304,7 +300,7 @@ func (s *ChatService) FindChatRoomMessagesByRoomID( RoomID: roomID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if len(rows) == 0 { @@ -335,7 +331,7 @@ func (s *ChatService) FindChatRoomMessagesByRoomID( RoomID: roomID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if len(rows) == 0 { @@ -368,7 +364,7 @@ func (s *ChatService) HasPrevMessages( RoomID: roomID, }) if err != nil { - return false, pnd.FromPostgresError(err) + return false, err } return hasPrev, nil @@ -383,7 +379,7 @@ func (s *ChatService) HasNextMessages( RoomID: roomID, }) if err != nil { - return false, pnd.FromPostgresError(err) + return false, err } return hasNext, nil diff --git a/internal/service/media_service.go b/internal/service/media_service.go index b508209..dedcd3e 100644 --- a/internal/service/media_service.go +++ b/internal/service/media_service.go @@ -13,7 +13,6 @@ import ( databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen" - pnd "github.com/pet-sitter/pets-next-door-api/api" "github.com/pet-sitter/pets-next-door-api/internal/domain/media" "github.com/pet-sitter/pets-next-door-api/internal/infra/database" ) @@ -65,7 +64,7 @@ func (s *MediaService) CreateMedia( Url: url, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -83,7 +82,7 @@ func (s *MediaService) FindMediaByID( ID: uuid.NullUUID{UUID: id, Valid: true}, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return media.ToDetailView(mediaData), nil @@ -106,7 +105,7 @@ func (s *MediaService) FindMediasByIDs( IncludeDeleted: false, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } views := make([]media.DetailView, 0) for _, mediaData := range mediaDataList { diff --git a/internal/service/sos_condition_service.go b/internal/service/sos_condition_service.go index 57a0f25..8c45b34 100644 --- a/internal/service/sos_condition_service.go +++ b/internal/service/sos_condition_service.go @@ -10,7 +10,6 @@ import ( "github.com/pet-sitter/pets-next-door-api/internal/domain/soscondition" databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen" - pnd "github.com/pet-sitter/pets-next-door-api/api" "github.com/pet-sitter/pets-next-door-api/internal/infra/database" ) @@ -41,7 +40,7 @@ func (service *SOSConditionService) InitConditions( Name: utils.StrToNullStr(conditionName.String()), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } conditionList[idx] = created @@ -59,7 +58,7 @@ func (service *SOSConditionService) FindConditions( ) (soscondition.ListView, error) { conditionList, err := databasegen.New(service.conn).FindConditions(ctx, false) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return soscondition.ToListViewFromRows(conditionList), nil diff --git a/internal/service/sos_post_service.go b/internal/service/sos_post_service.go index a32fc3c..9a64b05 100644 --- a/internal/service/sos_post_service.go +++ b/internal/service/sos_post_service.go @@ -6,7 +6,6 @@ import ( "time" "github.com/google/uuid" - pnd "github.com/pet-sitter/pets-next-door-api/api" utils "github.com/pet-sitter/pets-next-door-api/internal/common" "github.com/pet-sitter/pets-next-door-api/internal/datatype" "github.com/pet-sitter/pets-next-door-api/internal/domain/media" @@ -33,17 +32,18 @@ func (service *SOSPostService) WriteSOSPost( ctx context.Context, fbUID string, request *sospost.WriteSOSPostRequest, ) (*sospost.DetailView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() + q := databasegen.New(tx) userData, err := q.FindUser(ctx, databasegen.FindUserParams{ FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } thumbnailID := setThumbnailID(request.ImageIDs) @@ -61,24 +61,24 @@ func (service *SOSPostService) WriteSOSPost( ResourceType: utils.StrToNullStr(resourcemedia.SOSResourceType.String()), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } conditionList, err := q.FindSOSPostConditions(ctx, databasegen.FindSOSPostConditionsParams{ SosPostID: sosPost.ID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } petRows, err := q.FindPetsBySOSPostID(ctx, sosPost.ID) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } dates, err := q.FindDatesBySOSPostID(ctx, uuid.NullUUID{UUID: sosPost.ID, Valid: true}) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -115,7 +115,7 @@ func (service *SOSPostService) createSOSPost( sosPost, err := q.WriteSOSPost(ctx, params) if err != nil { - return databasegen.WriteSOSPostRow{}, pnd.FromPostgresError(err) + return databasegen.WriteSOSPostRow{}, err } return sosPost, nil @@ -143,10 +143,10 @@ func (service *SOSPostService) FindSOSPosts( ctx context.Context, page, size int, sortBy, filterType string, ) (*sospost.FindSOSPostListView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() sosPosts, err := databasegen.New(tx).FindSOSPosts(ctx, databasegen.FindSOSPostsParams{ EarliestDateStartAt: utils.FormatDateString(time.Now().String()), @@ -156,7 +156,7 @@ func (service *SOSPostService) FindSOSPosts( Offset: utils.IntToNullInt32((page - 1) * size), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } sosPostInfoList := sospost.ToInfoListFromFindRow(sosPosts, page, size) @@ -168,7 +168,7 @@ func (service *SOSPostService) FindSOSPosts( IncludeDeleted: true, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } sosPostView := sosPost.ToFindSOSPostInfoView( &user.WithoutPrivateInfo{ @@ -191,10 +191,10 @@ func (service *SOSPostService) FindSOSPostsByAuthorID( ctx context.Context, authorID uuid.UUID, page, size int, sortBy, filterType string, ) (*sospost.FindSOSPostListView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() sosPosts, err := databasegen.New(tx). FindSOSPostsByAuthorID(ctx, databasegen.FindSOSPostsByAuthorIDParams{ @@ -206,7 +206,7 @@ func (service *SOSPostService) FindSOSPostsByAuthorID( Offset: utils.IntToNullInt32((page - 1) * size), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } sosPostInfoList := sospost.ToInfoListFromFindAuthorIDRow(sosPosts, page, size) @@ -218,7 +218,7 @@ func (service *SOSPostService) FindSOSPostsByAuthorID( IncludeDeleted: true, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } sosPostView := sosPost.ToFindSOSPostInfoView( @@ -242,14 +242,14 @@ func (service *SOSPostService) FindSOSPostByID( ctx context.Context, id uuid.UUID, ) (*sospost.FindSOSPostView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() sosPost, err := databasegen.New(tx).FindSOSPostByID(ctx, uuid.NullUUID{UUID: id, Valid: true}) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } sosPostInfo := sospost.ToInfoFromFindByIDRow(sosPost) @@ -258,7 +258,7 @@ func (service *SOSPostService) FindSOSPostByID( IncludeDeleted: true, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -282,10 +282,11 @@ func (service *SOSPostService) UpdateSOSPost( ctx context.Context, request *sospost.UpdateSOSPostRequest, ) (*sospost.DetailView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() + q := databasegen.New(tx) if err = service.updateAllLinks(ctx, q, request); err != nil { @@ -304,7 +305,7 @@ func (service *SOSPostService) UpdateSOSPost( ResourceType: utils.StrToNullStr(resourcemedia.SOSResourceType.String()), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } conditionList, err := databasegen.New(tx). @@ -312,17 +313,17 @@ func (service *SOSPostService) UpdateSOSPost( SosPostID: updateSOSPost.ID, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } petRows, err := databasegen.New(tx).FindPetsBySOSPostID(ctx, updateSOSPost.ID) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } dates, err := q.FindDatesBySOSPostID(ctx, uuid.NullUUID{UUID: request.ID, Valid: true}) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -360,7 +361,7 @@ func (service *SOSPostService) updateSOSPost( updateSOSPost, err := q.UpdateSOSPost(ctx, params) if err != nil { - return databasegen.UpdateSOSPostRow{}, pnd.FromPostgresError(err) + return databasegen.UpdateSOSPostRow{}, err } return updateSOSPost, nil @@ -400,22 +401,22 @@ func (service *SOSPostService) CheckUpdatePermission( ctx context.Context, fbUID string, sosPostID uuid.UUID, ) (bool, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return false, err } + defer tx.Rollback() userData, err := databasegen.New(tx).FindUser(ctx, databasegen.FindUserParams{ FbUid: utils.StrToNullStr(fbUID), }) if err != nil { - return false, pnd.FromPostgresError(err) + return false, err } sosPost, err := databasegen.New(tx). FindSOSPostByID(ctx, uuid.NullUUID{UUID: sosPostID, Valid: true}) if err != nil { - return false, pnd.FromPostgresError(err) + return false, err } sosPostInfo := sospost.ToInfoFromFindByIDRow(sosPost) @@ -445,7 +446,7 @@ func (service *SOSPostService) SaveSOSDates( DateEndAt: dateEndAt, }) if err != nil { - return pnd.FromPostgresError(err) + return err } if err := tx.LinkSOSPostDate(ctx, databasegen.LinkSOSPostDateParams{ @@ -453,7 +454,7 @@ func (service *SOSPostService) SaveSOSDates( SosPostID: sosPostID, SosDatesID: d.ID, }); err != nil { - return pnd.FromPostgresError(err) + return err } } return nil @@ -471,7 +472,7 @@ func (service *SOSPostService) SaveLinkSOSPostImage( ResourceID: sosPostID, ResourceType: utils.StrToNullStr(resourcemedia.SOSResourceType.String()), }); err != nil { - return pnd.FromPostgresError(err) + return err } } return nil @@ -488,7 +489,7 @@ func (service *SOSPostService) SaveLinkConditions( SosPostID: sosPostID, SosConditionID: uuid.NullUUID{UUID: conditionID, Valid: true}, }); err != nil { - return pnd.FromPostgresError(err) + return err } } return nil @@ -505,7 +506,7 @@ func (service *SOSPostService) SaveLinkPets( SosPostID: sosPostID, PetID: petID, }); err != nil { - return pnd.FromPostgresError(err) + return err } } return nil @@ -514,37 +515,25 @@ func (service *SOSPostService) SaveLinkPets( func (service *SOSPostService) DeleteLinkSOSPostDates( ctx context.Context, tx *databasegen.Queries, sosPostID uuid.UUID, ) error { - if err := tx.DeleteSOSPostDateBySOSPostID(ctx, sosPostID); err != nil { - return pnd.FromPostgresError(err) - } - return nil + return tx.DeleteSOSPostDateBySOSPostID(ctx, sosPostID) } func (service *SOSPostService) DeleteLinkSOSPostImages( ctx context.Context, tx *databasegen.Queries, sosPostID uuid.UUID, ) error { - if err := tx.DeleteResourceMediaByResourceID(ctx, sosPostID); err != nil { - return pnd.FromPostgresError(err) - } - return nil + return tx.DeleteResourceMediaByResourceID(ctx, sosPostID) } func (service *SOSPostService) DeleteLinkSOSPostConditions( ctx context.Context, tx *databasegen.Queries, sosPostID uuid.UUID, ) error { - if err := tx.DeleteSOSPostConditionBySOSPostID(ctx, sosPostID); err != nil { - return pnd.FromPostgresError(err) - } - return nil + return tx.DeleteSOSPostConditionBySOSPostID(ctx, sosPostID) } func (service *SOSPostService) DeleteLinkSOSPostPets( ctx context.Context, tx *databasegen.Queries, sosPostID uuid.UUID, ) error { - if err := tx.DeleteSOSPostPetBySOSPostID(ctx, sosPostID); err != nil { - return pnd.FromPostgresError(err) - } - return nil + return tx.DeleteSOSPostPetBySOSPostID(ctx, sosPostID) } func setThumbnailID(imageIDs []uuid.UUID) uuid.NullUUID { diff --git a/internal/service/user_service.go b/internal/service/user_service.go index 67c7291..618576b 100644 --- a/internal/service/user_service.go +++ b/internal/service/user_service.go @@ -39,16 +39,16 @@ func (service *UserService) RegisterUser( } tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() _, err = databasegen.New(service.conn). WithTx(tx.Tx). CreateUser(ctx, registerUserRequest.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -59,7 +59,7 @@ func (service *UserService) RegisterUser( FbUid: utils.StrToNullStr(registerUserRequest.FirebaseUID), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return user.ToWithProfileImage(row).ToInternalView(), nil @@ -70,7 +70,7 @@ func (service *UserService) FindUsers( ) (*user.ListWithoutPrivateInfo, error) { rows, err := databasegen.New(service.conn).FindUsers(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return user.ToListWithoutPrivateInfo(params.Page, params.Size, rows), nil @@ -82,7 +82,7 @@ func (service *UserService) FindUser( ) (*user.WithProfileImage, error) { row, err := databasegen.New(service.conn).FindUser(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return user.ToWithProfileImage(row), nil @@ -93,7 +93,7 @@ func (service *UserService) FindUserProfile( ) (*user.ProfileView, error) { row, err := databasegen.New(service.conn).FindUser(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } petParams := pet.FindPetsParams{OwnerID: uuid.NullUUID{UUID: row.ID, Valid: true}} @@ -111,7 +111,7 @@ func (service *UserService) ExistsByNickname( ) (bool, error) { existsByNickname, err := databasegen.New(service.conn).ExistsUserByNickname(ctx, nickname) if err != nil { - return existsByNickname, pnd.FromPostgresError(err) + return existsByNickname, err } return existsByNickname, nil @@ -121,10 +121,10 @@ func (service *UserService) UpdateUserByUID( ctx context.Context, uid, nickname string, profileImageID uuid.NullUUID, ) (*user.MyProfileView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() _, err = databasegen.New(service.conn). WithTx(tx.Tx). @@ -134,7 +134,7 @@ func (service *UserService) UpdateUserByUID( FbUid: utils.StrToNullStr(uid), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err := tx.Commit(); err != nil { @@ -145,7 +145,7 @@ func (service *UserService) UpdateUserByUID( FbUid: utils.StrToNullStr(uid), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return user.ToWithProfileImage(refreshedUser).ToMyProfileView(), nil @@ -153,13 +153,13 @@ func (service *UserService) UpdateUserByUID( func (service *UserService) DeleteUserByUID(ctx context.Context, uid string) error { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return err } + defer tx.Rollback() if err := databasegen.New(service.conn).WithTx(tx.Tx).DeleteUserByFbUID(ctx, utils.StrToNullStr(uid)); err != nil { - return pnd.FromPostgresError(err) + return err } return tx.Commit() @@ -170,7 +170,7 @@ func (service *UserService) FindPet( ) (*pet.WithProfileImage, error) { row, err := databasegen.New(service.conn).FindPet(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return pet.ToWithProfileImage(row), nil @@ -182,7 +182,7 @@ func (service *UserService) FindPets( ) (*pet.ListView, error) { rows, err := databasegen.New(service.conn).FindPets(ctx, params.ToDBParams()) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return pet.ToListView(rows), nil @@ -192,17 +192,17 @@ func (service *UserService) AddPetsToOwner( ctx context.Context, uid string, addPetsRequest pet.AddPetsToOwnerRequest, ) (*pet.ListView, error) { tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() // 사용자가 존재하는지 확인 userData, err := databasegen.New(service.conn).FindUser(ctx, databasegen.FindUserParams{ FbUid: utils.StrToNullStr(uid), }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } // 프로필 이미지 ID가 DB에 존재하는지 확인 @@ -239,7 +239,7 @@ func (service *UserService) AddPetsToOwner( } row, err := databasegen.New(service.conn).WithTx(tx.Tx).CreatePet(ctx, petToCreate) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } petIDs = append(petIDs, row.ID) } @@ -252,7 +252,7 @@ func (service *UserService) AddPetsToOwner( Ids: petIDs, }) if err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } return pet.ToListViewFromIDsRows(rows), nil @@ -285,10 +285,10 @@ func (service *UserService) UpdatePet( } tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return nil, err } + defer tx.Rollback() birthDate, err := datatype.ParseDateToTime(updatePetRequest.BirthDate) if err != nil { @@ -305,7 +305,7 @@ func (service *UserService) UpdatePet( Remarks: updatePetRequest.Remarks, ProfileImageID: updatePetRequest.ProfileImageID, }); err != nil { - return nil, pnd.FromPostgresError(err) + return nil, err } if err = tx.Commit(); err != nil { @@ -345,13 +345,13 @@ func (service *UserService) DeletePet( } tx, err := service.conn.BeginTx(ctx) - defer tx.Rollback() if err != nil { return err } + defer tx.Rollback() if err := databasegen.New(service.conn).WithTx(tx.Tx).DeletePet(ctx, petID); err != nil { - return pnd.FromPostgresError(err) + return err } return tx.Commit()