Skip to content

Commit

Permalink
[AN/USER] Json convert시 생긴 에러 Catch 하도록 수정 (#509) (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
re4rk authored Oct 7, 2023
1 parent fa093ed commit 52e3142
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.festago.festago.data.repository

import com.festago.festago.data.service.UserRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.repository.AuthRepository
import com.festago.festago.repository.TokenRepository
import com.kakao.sdk.user.UserApiClient
Expand Down Expand Up @@ -29,16 +30,13 @@ class AuthDefaultRepository @Inject constructor(
return Result.success(Unit)
}

override suspend fun deleteAccount(): Result<Unit> {
userRetrofitService.deleteUserAccount().runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let {
override suspend fun deleteAccount(): Result<Unit> =
runCatchingResponse { userRetrofitService.deleteUserAccount() }
.onSuccessOrCatch {
UserApiClient.instance.unlink { error ->
if (error == null) {
tokenRepository.token = null
}
}
return Result.success(Unit)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.festago.festago.data.repository

import com.festago.festago.data.service.FestivalRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.Festival
import com.festago.festago.model.Reservation
import com.festago.festago.repository.FestivalRepository
Expand All @@ -10,15 +11,11 @@ import javax.inject.Inject
class FestivalDefaultRepository @Inject constructor(
private val festivalRetrofitService: FestivalRetrofitService,
) : FestivalRepository {
override suspend fun loadFestivals(): Result<List<Festival>> {
festivalRetrofitService.getFestivals().runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadFestivals(): Result<List<Festival>> =
runCatchingResponse { festivalRetrofitService.getFestivals() }
.onSuccessOrCatch { it.toDomain() }

override suspend fun loadFestivalDetail(festivalId: Long): Result<Reservation> {
festivalRetrofitService.getFestivalDetail(festivalId).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadFestivalDetail(festivalId: Long): Result<Reservation> =
runCatchingResponse { festivalRetrofitService.getFestivalDetail(festivalId) }
.onSuccessOrCatch { it.toDomain() }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.festago.festago.data.repository

import com.festago.festago.data.service.ReservationTicketRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.ReservationTicket
import com.festago.festago.repository.ReservationTicketRepository
import javax.inject.Inject
Expand All @@ -10,10 +11,7 @@ class ReservationTicketDefaultRepository @Inject constructor(
private val reservationTicketRetrofitService: ReservationTicketRetrofitService,
) : ReservationTicketRepository {

override suspend fun loadTicketTypes(stageId: Int): Result<List<ReservationTicket>> {
reservationTicketRetrofitService.getReservationTickets(stageId)
.runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadTicketTypes(stageId: Int): Result<List<ReservationTicket>> =
runCatchingResponse { reservationTicketRetrofitService.getReservationTickets(stageId) }
.onSuccessOrCatch { it.toDomain() }
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.festago.festago.data.repository

import com.festago.festago.data.service.SchoolRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.School
import com.festago.festago.repository.SchoolRepository
import javax.inject.Inject

class SchoolDefaultRepository @Inject constructor(
private val schoolRetrofitService: SchoolRetrofitService
private val schoolRetrofitService: SchoolRetrofitService,
) : SchoolRepository {

override suspend fun loadSchools(): Result<List<School>> {
schoolRetrofitService.getSchools()
.runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadSchools(): Result<List<School>> =
runCatchingResponse { schoolRetrofitService.getSchools() }
.onSuccessOrCatch { it.toDomain() }

override suspend fun loadSchoolEmail(schoolId: Long): Result<String> {
// TODO: API 연동 작업 필요
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.festago.festago.data.repository
import com.festago.festago.data.dto.SendVerificationRequest
import com.festago.festago.data.dto.VerificationRequest
import com.festago.festago.data.service.StudentVerificationRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.StudentVerificationCode
import com.festago.festago.repository.StudentVerificationRepository
import javax.inject.Inject
Expand All @@ -12,19 +12,17 @@ class StudentVerificationDefaultRepository @Inject constructor(
private val studentVerificationRetrofitService: StudentVerificationRetrofitService,
) : StudentVerificationRepository {

override suspend fun sendVerificationCode(userName: String, schoolId: Long): Result<Unit> {
studentVerificationRetrofitService.sendVerificationCode(
SendVerificationRequest(userName, schoolId),
).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(Unit) }
}
override suspend fun sendVerificationCode(userName: String, schoolId: Long): Result<Unit> =
runCatchingResponse {
studentVerificationRetrofitService.sendVerificationCode(
SendVerificationRequest(userName, schoolId),
)
}

override suspend fun requestVerificationCodeConfirm(code: StudentVerificationCode): Result<Unit> {
studentVerificationRetrofitService.requestVerification(
VerificationRequest.from(code),
).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(Unit) }
}
override suspend fun requestVerificationCodeConfirm(code: StudentVerificationCode): Result<Unit> =
runCatchingResponse {
studentVerificationRetrofitService.requestVerification(
VerificationRequest.from(code),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.festago.festago.data.repository

import com.festago.festago.data.dto.ReservedTicketRequest
import com.festago.festago.data.service.TicketRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.ReservedTicket
import com.festago.festago.model.Ticket
import com.festago.festago.model.TicketCode
Expand All @@ -13,34 +14,26 @@ class TicketDefaultRepository @Inject constructor(
private val ticketRetrofitService: TicketRetrofitService,
) : TicketRepository {

override suspend fun loadTicket(ticketId: Long): Result<Ticket> {
ticketRetrofitService.getTicket(ticketId).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadTicket(ticketId: Long): Result<Ticket> =
runCatchingResponse { ticketRetrofitService.getTicket(ticketId) }
.onSuccessOrCatch { it.toDomain() }

override suspend fun loadCurrentTickets(): Result<List<Ticket>> {
ticketRetrofitService.getCurrentTickets().runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadCurrentTickets(): Result<List<Ticket>> =
runCatchingResponse { ticketRetrofitService.getCurrentTickets() }
.onSuccessOrCatch { it.toDomain() }

override suspend fun loadTicketCode(ticketId: Long): Result<TicketCode> {
ticketRetrofitService.getTicketCode(ticketId).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadTicketCode(ticketId: Long): Result<TicketCode> =
runCatchingResponse { ticketRetrofitService.getTicketCode(ticketId) }
.onSuccessOrCatch { it.toDomain() }

override suspend fun loadHistoryTickets(size: Int): Result<List<Ticket>> {
ticketRetrofitService.getHistoryTickets(size).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadHistoryTickets(size: Int): Result<List<Ticket>> =
runCatchingResponse { ticketRetrofitService.getHistoryTickets(size) }
.onSuccessOrCatch { it.toDomain() }

override suspend fun reserveTicket(ticketId: Int): Result<ReservedTicket> {
ticketRetrofitService.postReserveTicket(ReservedTicketRequest(ticketId))
.runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun reserveTicket(ticketId: Int): Result<ReservedTicket> =
runCatchingResponse {
ticketRetrofitService.postReserveTicket(
ReservedTicketRequest(ticketId),
)
}.onSuccessOrCatch { it.toDomain() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.festago.festago.data.repository
import com.festago.festago.data.datasource.TokenDataSource
import com.festago.festago.data.dto.OauthRequest
import com.festago.festago.data.service.TokenRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.repository.TokenRepository
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
Expand All @@ -18,23 +19,14 @@ class TokenDefaultRepository @Inject constructor(
tokenLocalDataSource.token = value
}

override suspend fun signIn(socialType: String, token: String): Result<Unit> {
tokenRetrofitService.getOauthToken(OauthRequest(socialType, token))
.runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let {
tokenLocalDataSource.token = it.accessToken
return Result.success(Unit)
}
}
override suspend fun signIn(socialType: String, token: String): Result<Unit> =
runCatchingResponse {
tokenRetrofitService.getOauthToken(OauthRequest(socialType, token))
}.onSuccessOrCatch { tokenLocalDataSource.token = it.accessToken }

override fun refreshToken(token: String): Result<Unit> = runBlocking {
tokenRetrofitService.getOauthToken(OauthRequest("KAKAO", token))
.runCatchingWithErrorHandler()
.getOrElse { error -> return@runBlocking Result.failure(error) }
.let {
tokenLocalDataSource.token = it.accessToken
return@runBlocking Result.success(Unit)
}
runCatchingResponse {
tokenRetrofitService.getOauthToken(OauthRequest("KAKAO", token))
}.onSuccessOrCatch { tokenLocalDataSource.token = it.accessToken }
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.festago.festago.data.repository

import com.festago.festago.data.service.UserRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.data.util.onSuccessOrCatch
import com.festago.festago.data.util.runCatchingResponse
import com.festago.festago.model.UserProfile
import com.festago.festago.repository.UserRepository
import javax.inject.Inject

class UserDefaultRepository @Inject constructor(
private val userProfileService: UserRetrofitService,
) : UserRepository {
override suspend fun loadUserProfile(): Result<UserProfile> {
userProfileService.getUserProfile().runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(it.toDomain()) }
}
override suspend fun loadUserProfile(): Result<UserProfile> =
runCatchingResponse { userProfileService.getUserProfile() }
.onSuccessOrCatch { it.toDomain() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package com.festago.festago.data.util

import retrofit2.Response

fun <T> Response<T>.runCatchingWithErrorHandler(): Result<T> {
suspend fun <T> runCatchingResponse(
block: suspend () -> Response<T>,
): Result<T> {
try {
if (this.isSuccessful && this.body() != null) {
return Result.success(this.body()!!)
val response = block()
if (response.isSuccessful && response.body() != null) {
return Result.success(response.body()!!)
}

return Result.failure(
Throwable(
"{" +
"code: ${this.code()}," +
"message: ${this.message()}, " +
"body: ${this.errorBody()?.string()}" +
"code: ${response.code()}," +
"message: ${response.message()}, " +
"body: ${response.errorBody()?.string()}" +
"}",
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.festago.festago.data.util

suspend fun <T, R> Result<T>.onSuccessOrCatch(block: suspend (T) -> R): Result<R> {
return try {
onSuccess { return Result.success(block(it)) }
onFailure { return Result.failure(it) }

throw Throwable("This line should not be reached")
} catch (e: Exception) {
Result.failure(e)
}
}

0 comments on commit 52e3142

Please sign in to comment.