Skip to content

Commit

Permalink
�모임 에러 핸들링 (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinuemong authored Oct 1, 2024
1 parent d01080a commit 1d8f222
Show file tree
Hide file tree
Showing 40 changed files with 507 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.happy.friendogly.data.error

import com.happy.friendogly.data.mapper.toDomain
import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import java.net.ConnectException
import java.net.UnknownHostException

fun <T> Throwable.toDomainError(): DomainResult.Error<T, DataError.Network> {
return when (this) {
is ApiExceptionDto -> DomainResult.Error(this.error.data.errorCode.toDomain())
is ConnectException, is UnknownHostException -> DomainResult.Error(DataError.Network.NO_INTERNET)
else -> DomainResult.Error(DataError.Network.SERVER_ERROR)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.happy.friendogly.data.repository

import com.happy.friendogly.data.error.toDomainError
import com.happy.friendogly.data.mapper.toData
import com.happy.friendogly.data.mapper.toDomain
import com.happy.friendogly.data.source.ClubDataSource
import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.model.ClubAddress
import com.happy.friendogly.domain.model.ClubDetail
Expand All @@ -29,8 +32,8 @@ class ClubRepositoryImpl
memberCapacity: Int,
file: MultipartBody.Part?,
petIds: List<Long>,
): Result<Unit> =
source.postClub(
): DomainResult<Unit, DataError.Network> {
return source.postClub(
title = title,
content = content,
address = address.toData(),
Expand All @@ -39,44 +42,94 @@ class ClubRepositoryImpl
memberCapacity = memberCapacity,
file = file,
petIds = petIds,
).fold(
onSuccess = { result ->
DomainResult.Success(result)
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun getSearchingClubs(
filterCondition: ClubFilterCondition,
address: ClubAddress,
genderParams: List<Gender>,
sizeParams: List<SizeType>,
): Result<List<Club>> =
source.getSearchingClubs(
): DomainResult<List<Club>, DataError.Network> {
return source.getSearchingClubs(
filterCondition = filterCondition.toData(),
address = address.toData(),
genderParams = genderParams.map { it.toData() },
sizeParams = sizeParams.map { it.toData() },
).mapCatching { it.toDomain() }
).fold(
onSuccess = { clubs ->
DomainResult.Success(clubs.toDomain())
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun getClub(clubId: Long): Result<ClubDetail> = source.getClub(clubId).mapCatching { it.toDomain() }
override suspend fun getClub(clubId: Long): DomainResult<ClubDetail, DataError.Network> {
return source.getClub(clubId).fold(
onSuccess = { clubDetail ->
DomainResult.Success(clubDetail.toDomain())
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun postClubMember(
clubId: Long,
participatingPetsId: List<Long>,
): Result<ClubParticipation> =
source.postClubMember(
): DomainResult<ClubParticipation, DataError.Network> {
return source.postClubMember(
clubId = clubId,
participatingPetsId = participatingPetsId,
).mapCatching { it.toDomain() }
).fold(
onSuccess = { clubParticipation ->
DomainResult.Success(clubParticipation.toDomain())
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun deleteClubMember(clubId: Long): Result<Unit> = source.deleteClubMember(clubId)
override suspend fun deleteClubMember(clubId: Long): DomainResult<Unit, DataError.Network> {
return source.deleteClubMember(clubId).fold(
onSuccess = { result ->
DomainResult.Success(result)
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun patchClub(
clubId: Long,
title: String,
content: String,
state: ClubState,
): Result<Unit> =
source.patchClub(
): DomainResult<Unit, DataError.Network> {
return source.patchClub(
clubId = clubId,
title = title,
content = content,
state = state.toData(),
).fold(
onSuccess = { result ->
DomainResult.Success(result)
},
onFailure = { error ->
error.toDomainError()
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.happy.friendogly.data.repository

import com.happy.friendogly.data.error.toDomainError
import com.happy.friendogly.data.mapper.toDomain
import com.happy.friendogly.data.source.MyClubDataSource
import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.repository.MyClubRepository
import javax.inject.Inject
Expand All @@ -11,7 +14,25 @@ class MyClubRepositoryImpl
constructor(
private val source: MyClubDataSource,
) : MyClubRepository {
override suspend fun getMyClubs(): Result<List<Club>> = source.getParticipatingClubs().mapCatching { it.toDomain() }
override suspend fun getMyClubs(): DomainResult<List<Club>, DataError.Network> {
return source.getParticipatingClubs().fold(
onSuccess = { clubs ->
DomainResult.Success(clubs.toDomain())
},
onFailure = { error ->
error.toDomainError()
},
)
}

override suspend fun getMyHeadClubs(): Result<List<Club>> = source.getMyOwningClubs().mapCatching { it.toDomain() }
override suspend fun getMyHeadClubs(): DomainResult<List<Club>, DataError.Network> {
return source.getMyOwningClubs().fold(
onSuccess = { clubs ->
DomainResult.Success(clubs.toDomain())
},
onFailure = { error ->
error.toDomainError()
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.repository

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.model.ClubAddress
import com.happy.friendogly.domain.model.ClubDetail
Expand All @@ -20,28 +22,28 @@ interface ClubRepository {
memberCapacity: Int,
file: MultipartBody.Part?,
petIds: List<Long>,
): Result<Unit>
): DomainResult<Unit, DataError.Network>

suspend fun getSearchingClubs(
filterCondition: ClubFilterCondition,
address: ClubAddress,
genderParams: List<Gender>,
sizeParams: List<SizeType>,
): Result<List<Club>>
): DomainResult<List<Club>, DataError.Network>

suspend fun getClub(clubId: Long): Result<ClubDetail>
suspend fun getClub(clubId: Long): DomainResult<ClubDetail, DataError.Network>

suspend fun postClubMember(
clubId: Long,
participatingPetsId: List<Long>,
): Result<ClubParticipation>
): DomainResult<ClubParticipation, DataError.Network>

suspend fun deleteClubMember(clubId: Long): Result<Unit>
suspend fun deleteClubMember(clubId: Long): DomainResult<Unit, DataError.Network>

suspend fun patchClub(
clubId: Long,
title: String,
content: String,
state: ClubState,
): Result<Unit>
): DomainResult<Unit, DataError.Network>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.happy.friendogly.domain.repository

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club

interface MyClubRepository {
suspend fun getMyClubs(): Result<List<Club>>
suspend fun getMyClubs(): DomainResult<List<Club>, DataError.Network>

suspend fun getMyHeadClubs(): Result<List<Club>>
suspend fun getMyHeadClubs(): DomainResult<List<Club>, DataError.Network>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.repository.ClubRepository
import javax.inject.Inject

Expand All @@ -8,5 +10,5 @@ class DeleteClubMemberUseCase
constructor(
private val repository: ClubRepository,
) {
suspend operator fun invoke(id: Long): Result<Unit> = repository.deleteClubMember(clubId = id)
suspend operator fun invoke(id: Long): DomainResult<Unit, DataError.Network> = repository.deleteClubMember(clubId = id)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.ClubDetail
import com.happy.friendogly.domain.repository.ClubRepository
import javax.inject.Inject
Expand All @@ -9,5 +11,5 @@ class GetClubUseCase
constructor(
private val repository: ClubRepository,
) {
suspend operator fun invoke(id: Long): Result<ClubDetail> = repository.getClub(clubId = id)
suspend operator fun invoke(id: Long): DomainResult<ClubDetail, DataError.Network> = repository.getClub(clubId = id)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.repository.MyClubRepository
import javax.inject.Inject
Expand All @@ -9,5 +11,5 @@ class GetMyClubUseCase
constructor(
private val repository: MyClubRepository,
) {
suspend operator fun invoke(): Result<List<Club>> = repository.getMyClubs()
suspend operator fun invoke(): DomainResult<List<Club>, DataError.Network> = repository.getMyClubs()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.repository.MyClubRepository
import javax.inject.Inject
Expand All @@ -9,5 +11,5 @@ class GetMyHeadClubUseCase
constructor(
private val repository: MyClubRepository,
) {
suspend operator fun invoke(): Result<List<Club>> = repository.getMyHeadClubs()
suspend operator fun invoke(): DomainResult<List<Club>, DataError.Network> = repository.getMyHeadClubs()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.Club
import com.happy.friendogly.domain.model.ClubAddress
import com.happy.friendogly.domain.model.ClubFilterCondition
Expand All @@ -18,7 +20,7 @@ class GetSearchingClubsUseCase
address: ClubAddress,
genderParams: List<Gender>,
sizeParams: List<SizeType>,
): Result<List<Club>> =
): DomainResult<List<Club>, DataError.Network> =
repository.getSearchingClubs(
filterCondition = filterCondition,
address = address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.ClubState
import com.happy.friendogly.domain.repository.ClubRepository
import javax.inject.Inject
Expand All @@ -12,7 +14,7 @@ class PatchClubUseCase
title: String,
content: String,
state: ClubState,
): Result<Unit> =
): DomainResult<Unit, DataError.Network> =
repository.patchClub(
clubId = clubId,
title = title,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.happy.friendogly.domain.usecase

import com.happy.friendogly.domain.DomainResult
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.model.ClubAddress
import com.happy.friendogly.domain.model.Gender
import com.happy.friendogly.domain.model.SizeType
Expand All @@ -21,7 +23,7 @@ class PostClubUseCase
memberCapacity: Int,
file: MultipartBody.Part?,
petIds: List<Long>,
): Result<Unit> =
): DomainResult<Unit, DataError.Network> =
repository.postClub(
title = title,
content = content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import com.happy.friendogly.presentation.base.BaseActivity
import com.happy.friendogly.presentation.base.observeEvent
import com.happy.friendogly.presentation.ui.club.add.adapter.ClubAddAdapter
import com.happy.friendogly.presentation.ui.club.common.ClubChangeStateIntent
import com.happy.friendogly.presentation.ui.club.common.MessageHandler
import com.happy.friendogly.presentation.ui.club.common.handleError
import com.happy.friendogly.presentation.ui.club.common.model.clubfilter.ClubFilter
import com.happy.friendogly.presentation.ui.club.select.PetSelectBottomSheet
import com.happy.friendogly.presentation.ui.profilesetting.bottom.EditProfileImageBottomSheet
Expand Down Expand Up @@ -99,13 +101,28 @@ class ClubAddActivity : BaseActivity<ActivityClubAddBinding>(R.layout.activity_c
}

ClubAddEvent.FailLoadAddress -> showSnackbar(getString(R.string.club_add_information_fail_address))
ClubAddEvent.FailAddClub -> showSnackbar(getString(R.string.club_add_fail))
ClubAddEvent.Navigation.NavigateToHomeWithAdded -> {
putLoadState()
finish()
}
}
}

viewModel.clubErrorHandler.error.observeEvent(this@ClubAddActivity) {
it.handleError { message ->
when (message) {
is MessageHandler.SendSnackBar -> {
showSnackbar(getString(message.messageId)) {
setAction(resources.getString(R.string.club_detail_fail_button)) {
finish()
}
}
}

is MessageHandler.SendToast -> showToastMessage(getString(message.messageId))
}
}
}
}

private fun putLoadState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ sealed interface ClubAddEvent {
data class ChangePage(val page: Int) : ClubAddEvent

data object FailLoadAddress : ClubAddEvent

data object FailAddClub : ClubAddEvent
}
Loading

0 comments on commit 1d8f222

Please sign in to comment.