From 52e3142afb1378b7d32822f4ea968b832392ba15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=84=ED=81=AC?= <37167652+re4rk@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:29:18 +0900 Subject: [PATCH] =?UTF-8?q?[AN/USER]=20Json=20convert=EC=8B=9C=20=EC=83=9D?= =?UTF-8?q?=EA=B8=B4=20=EC=97=90=EB=9F=AC=20Catch=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=20(#509)=20(#510)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/repository/AuthDefaultRepository.kt | 12 ++--- .../repository/FestivalDefaultRepository.kt | 19 ++++---- .../ReservationTicketDefaultRepository.kt | 12 ++--- .../repository/SchoolDefaultRepository.kt | 14 +++--- .../StudentVerificationDefaultRepository.kt | 28 +++++------ .../repository/TicketDefaultRepository.kt | 47 ++++++++----------- .../data/repository/TokenDefaultRepository.kt | 26 ++++------ .../data/repository/UserDefaultRepository.kt | 11 ++--- .../festago/festago/data/util/ResponseExt.kt | 15 +++--- .../festago/festago/data/util/ResultExt.kt | 12 +++++ 10 files changed, 92 insertions(+), 104 deletions(-) create mode 100644 android/festago/app/src/main/java/com/festago/festago/data/util/ResultExt.kt diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/AuthDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/AuthDefaultRepository.kt index 1a793f5c0..e218672f2 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/AuthDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/AuthDefaultRepository.kt @@ -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 @@ -29,16 +30,13 @@ class AuthDefaultRepository @Inject constructor( return Result.success(Unit) } - override suspend fun deleteAccount(): Result { - userRetrofitService.deleteUserAccount().runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { + override suspend fun deleteAccount(): Result = + runCatchingResponse { userRetrofitService.deleteUserAccount() } + .onSuccessOrCatch { UserApiClient.instance.unlink { error -> if (error == null) { tokenRepository.token = null } } - return Result.success(Unit) } - } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/FestivalDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/FestivalDefaultRepository.kt index 935064243..12326dd03 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/FestivalDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/FestivalDefaultRepository.kt @@ -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 @@ -10,15 +11,11 @@ import javax.inject.Inject class FestivalDefaultRepository @Inject constructor( private val festivalRetrofitService: FestivalRetrofitService, ) : FestivalRepository { - override suspend fun loadFestivals(): Result> { - festivalRetrofitService.getFestivals().runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadFestivals(): Result> = + runCatchingResponse { festivalRetrofitService.getFestivals() } + .onSuccessOrCatch { it.toDomain() } - override suspend fun loadFestivalDetail(festivalId: Long): Result { - festivalRetrofitService.getFestivalDetail(festivalId).runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadFestivalDetail(festivalId: Long): Result = + runCatchingResponse { festivalRetrofitService.getFestivalDetail(festivalId) } + .onSuccessOrCatch { it.toDomain() } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/ReservationTicketDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/ReservationTicketDefaultRepository.kt index dd9a57656..dd957a56e 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/ReservationTicketDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/ReservationTicketDefaultRepository.kt @@ -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 @@ -10,10 +11,7 @@ class ReservationTicketDefaultRepository @Inject constructor( private val reservationTicketRetrofitService: ReservationTicketRetrofitService, ) : ReservationTicketRepository { - override suspend fun loadTicketTypes(stageId: Int): Result> { - reservationTicketRetrofitService.getReservationTickets(stageId) - .runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadTicketTypes(stageId: Int): Result> = + runCatchingResponse { reservationTicketRetrofitService.getReservationTickets(stageId) } + .onSuccessOrCatch { it.toDomain() } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/SchoolDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/SchoolDefaultRepository.kt index c0b5f93f3..fdca87e05 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/SchoolDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/SchoolDefaultRepository.kt @@ -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> { - schoolRetrofitService.getSchools() - .runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadSchools(): Result> = + runCatchingResponse { schoolRetrofitService.getSchools() } + .onSuccessOrCatch { it.toDomain() } override suspend fun loadSchoolEmail(schoolId: Long): Result { // TODO: API 연동 작업 필요 diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/StudentVerificationDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/StudentVerificationDefaultRepository.kt index 5136ac8c2..becc28334 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/StudentVerificationDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/StudentVerificationDefaultRepository.kt @@ -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 @@ -12,19 +12,17 @@ class StudentVerificationDefaultRepository @Inject constructor( private val studentVerificationRetrofitService: StudentVerificationRetrofitService, ) : StudentVerificationRepository { - override suspend fun sendVerificationCode(userName: String, schoolId: Long): Result { - 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 = + runCatchingResponse { + studentVerificationRetrofitService.sendVerificationCode( + SendVerificationRequest(userName, schoolId), + ) + } - override suspend fun requestVerificationCodeConfirm(code: StudentVerificationCode): Result { - studentVerificationRetrofitService.requestVerification( - VerificationRequest.from(code), - ).runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(Unit) } - } + override suspend fun requestVerificationCodeConfirm(code: StudentVerificationCode): Result = + runCatchingResponse { + studentVerificationRetrofitService.requestVerification( + VerificationRequest.from(code), + ) + } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/TicketDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/TicketDefaultRepository.kt index 12a43636c..33ac652e5 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/TicketDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/TicketDefaultRepository.kt @@ -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 @@ -13,34 +14,26 @@ class TicketDefaultRepository @Inject constructor( private val ticketRetrofitService: TicketRetrofitService, ) : TicketRepository { - override suspend fun loadTicket(ticketId: Long): Result { - ticketRetrofitService.getTicket(ticketId).runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadTicket(ticketId: Long): Result = + runCatchingResponse { ticketRetrofitService.getTicket(ticketId) } + .onSuccessOrCatch { it.toDomain() } - override suspend fun loadCurrentTickets(): Result> { - ticketRetrofitService.getCurrentTickets().runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadCurrentTickets(): Result> = + runCatchingResponse { ticketRetrofitService.getCurrentTickets() } + .onSuccessOrCatch { it.toDomain() } - override suspend fun loadTicketCode(ticketId: Long): Result { - ticketRetrofitService.getTicketCode(ticketId).runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadTicketCode(ticketId: Long): Result = + runCatchingResponse { ticketRetrofitService.getTicketCode(ticketId) } + .onSuccessOrCatch { it.toDomain() } - override suspend fun loadHistoryTickets(size: Int): Result> { - ticketRetrofitService.getHistoryTickets(size).runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadHistoryTickets(size: Int): Result> = + runCatchingResponse { ticketRetrofitService.getHistoryTickets(size) } + .onSuccessOrCatch { it.toDomain() } - override suspend fun reserveTicket(ticketId: Int): Result { - ticketRetrofitService.postReserveTicket(ReservedTicketRequest(ticketId)) - .runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun reserveTicket(ticketId: Int): Result = + runCatchingResponse { + ticketRetrofitService.postReserveTicket( + ReservedTicketRequest(ticketId), + ) + }.onSuccessOrCatch { it.toDomain() } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/TokenDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/TokenDefaultRepository.kt index 1f3d083b5..f65299b37 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/TokenDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/TokenDefaultRepository.kt @@ -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 @@ -18,23 +19,14 @@ class TokenDefaultRepository @Inject constructor( tokenLocalDataSource.token = value } - override suspend fun signIn(socialType: String, token: String): Result { - 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 = + runCatchingResponse { + tokenRetrofitService.getOauthToken(OauthRequest(socialType, token)) + }.onSuccessOrCatch { tokenLocalDataSource.token = it.accessToken } override fun refreshToken(token: String): Result = 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 } } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/repository/UserDefaultRepository.kt b/android/festago/app/src/main/java/com/festago/festago/data/repository/UserDefaultRepository.kt index 692d5dd18..354366dfc 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/repository/UserDefaultRepository.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/repository/UserDefaultRepository.kt @@ -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.model.UserProfile import com.festago.festago.repository.UserRepository import javax.inject.Inject @@ -9,9 +10,7 @@ import javax.inject.Inject class UserDefaultRepository @Inject constructor( private val userProfileService: UserRetrofitService, ) : UserRepository { - override suspend fun loadUserProfile(): Result { - userProfileService.getUserProfile().runCatchingWithErrorHandler() - .getOrElse { error -> return Result.failure(error) } - .let { return Result.success(it.toDomain()) } - } + override suspend fun loadUserProfile(): Result = + runCatchingResponse { userProfileService.getUserProfile() } + .onSuccessOrCatch { it.toDomain() } } diff --git a/android/festago/app/src/main/java/com/festago/festago/data/util/ResponseExt.kt b/android/festago/app/src/main/java/com/festago/festago/data/util/ResponseExt.kt index 750ace4c3..8203cc483 100644 --- a/android/festago/app/src/main/java/com/festago/festago/data/util/ResponseExt.kt +++ b/android/festago/app/src/main/java/com/festago/festago/data/util/ResponseExt.kt @@ -2,18 +2,21 @@ package com.festago.festago.data.util import retrofit2.Response -fun Response.runCatchingWithErrorHandler(): Result { +suspend fun runCatchingResponse( + block: suspend () -> Response, +): Result { 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()}" + "}", ), ) diff --git a/android/festago/app/src/main/java/com/festago/festago/data/util/ResultExt.kt b/android/festago/app/src/main/java/com/festago/festago/data/util/ResultExt.kt new file mode 100644 index 000000000..bc058d668 --- /dev/null +++ b/android/festago/app/src/main/java/com/festago/festago/data/util/ResultExt.kt @@ -0,0 +1,12 @@ +package com.festago.festago.data.util + +suspend fun Result.onSuccessOrCatch(block: suspend (T) -> R): Result { + 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) + } +}