Skip to content

Commit

Permalink
에러핸들러 추가 (#120)
Browse files Browse the repository at this point in the history
* ResultCallAdapter 추가하여 에러 파싱

* 시험 적용

* package 변경

* 축제 기능 추가 (#115)

* 레이아웃에 토글버튼 추가

* 뷰모델에 플래그와 토글함수 추가

* 뷰모델 플래그와 뷰 연결

* 메뉴 필터링

* 날짜에 따른 필터링 여부 조정

* lint

* 토글버튼 디자인 변경 반영

* 날짜 수정

* 홍보 배너 추가 (내용물과 링크는 임시)

* 홍보 배너 완성

* 날짜 플래그 최종 수정

* vm.showFestival에 observe 부착

* Revert "축제 기능 추가 (#115)"

This reverts commit 194d4de.

* fix typo

* 에러메시지 기본값 추가

* NetworkResult.map() 추가

* GET /community/posts/{post_id}에 에러핸들러 적용

* NetworkResult에 분기용 메서드 추가

* GET /community/boards/{board_id}에 에러핸들러 적용

* 임시커밋

* GET /community/posts에 에러핸들러 적용

* GET /menus/lo 에 에러핸들러 적용

* 의문의 쓰레기값 수정

* GET /reviews/ 에 에러핸들러 적용

* GET /reviews/filter 에 에러핸들러 적용

* GET /restaurants 에 에러핸들러 적용

* ErrorDto 수정 (detail -> message)

* GET /reviews, /reviews/images 에 에러핸들러 적용

* POST /auth/login 에 에러핸들러 적용

* POST /auth/refresh 에 에러핸들러 적용

* GET /reviews/comments/recommendation 에 에러핸들러 적용

* GET /reviews/dist 에 에러핸들러 적용

* POST /voc 에 에러핸들러 적용

* GET /auth/me/image 에 에러핸들러 적용

* PATCH /auth/me/image/profile, GET /auth/nicknames/validate 에 에러핸들러 적용

* GET /versions/android 에 에러핸들러 적용

* POST /menus/{menu_id}/like, /menus/{menu_id}/unlike 에 에러핸들러 적용

* GET /community/boards 에 에러핸들러 적용

* GET /community/posts/me 에 에러핸들러 적용

* POST /community/comments 에 에러핸들러 적용

* POST /community/posts/{post_id}/like, /community/posts/{post_id}/unlike 에 에러핸들러 적용

* POST, PATCH /community/posts 에 에러핸들러 적용

* POST /community/comments/{comment_id}/like, /community/comments/{comment_id}/unlike 에 에러핸들러 적용

* POST /community/posts/{post_id}/report, /community/comments/{comment_id}/report 에 에러핸들러 적용

* GET /community/posts/popular/trending 에 에러핸들러 적용

---------

Co-authored-by: eastshine2741 <[email protected]>
  • Loading branch information
wjshim2003 and eastshine2741 authored Dec 29, 2024
1 parent f8962e5 commit c696f8b
Show file tree
Hide file tree
Showing 33 changed files with 753 additions and 458 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ fun PostDetailRoute(
postDetailViewModel: PostDetailViewModel = hiltViewModel()
) {
val postUiState by postDetailViewModel.postUiState.collectAsState()
val board by postDetailViewModel.board.collectAsState()
val comments = postDetailViewModel.commentPagingData.collectAsLazyPagingItems()
val isAnonymous by postDetailViewModel.isAnonymous.collectAsState()

when (postUiState) {
is PostUiState.Success -> {
PostDetailScreenSuccess(
post = (postUiState as PostUiState.Success).post,
board = board,
board = (postUiState as PostUiState.Success).board,
comments = comments,
postDetailEvent = postDetailViewModel.postDetailEvent,
isAnonymous = isAnonymous,
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/wafflestudio/siksha2/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.squareup.moshi.Moshi
import com.wafflestudio.siksha2.BuildConfig
import com.wafflestudio.siksha2.R
import com.wafflestudio.siksha2.network.SikshaApi
import com.wafflestudio.siksha2.network.result.ResultCallAdapterFactory
import com.wafflestudio.siksha2.preferences.SikshaPrefObjects
import com.wafflestudio.siksha2.preferences.serializer.Serializer
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -48,10 +50,16 @@ object NetworkModule {

@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient, moshi: Moshi, @ApplicationContext context: Context): Retrofit {
fun provideRetrofit(
okHttpClient: OkHttpClient,
moshi: Moshi,
@ApplicationContext context: Context,
serializer: Serializer
): Retrofit {
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl(context.getString(R.string.server_base_url))
.addCallAdapterFactory(ResultCallAdapterFactory(serializer))
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
}
Expand Down
71 changes: 36 additions & 35 deletions app/src/main/java/com/wafflestudio/siksha2/network/SikshaApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wafflestudio.siksha2.network

import com.wafflestudio.siksha2.models.Menu
import com.wafflestudio.siksha2.network.dto.*
import com.wafflestudio.siksha2.network.result.NetworkResult
import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.*
Expand All @@ -12,31 +13,31 @@ interface SikshaApi {
suspend fun fetchMenuGroups(
@Query("start_date") startDate: LocalDate,
@Query("end_date") endDate: LocalDate
): FetchMenuGroupsResult
): NetworkResult<FetchMenuGroupsResult>

@GET("/menus/{menu_id}")
suspend fun fetchMenuById(@Path(value = "menu_id") menuId: Long): Menu
suspend fun fetchMenuById(@Path(value = "menu_id") menuId: Long): NetworkResult<Menu>

@GET("/reviews/")
suspend fun fetchReviews(
@Query("menu_id") menuId: Long,
@Query("page") page: Long,
@Query("per_page") perPage: Long
): FetchReviewsResult
): NetworkResult<FetchReviewsResult>

@GET("/reviews/filter")
suspend fun fetchReviewsWithImage(
@Query("menu_id") menuId: Long,
@Query("page") page: Long,
@Query("per_page") perPage: Long,
@Query("etc") etc: Boolean = true
): FetchReviewsResult
): NetworkResult<FetchReviewsResult>

@GET("/restaurants/")
suspend fun fetchRestaurants(): FetchRestaurantsResult
suspend fun fetchRestaurants(): NetworkResult<FetchRestaurantsResult>

@POST("/reviews/")
suspend fun leaveMenuReview(@Body req: LeaveReviewParam): LeaveReviewResult
suspend fun leaveMenuReview(@Body req: LeaveReviewParam): NetworkResult<LeaveReviewResult>

@Multipart
@POST("/reviews/images")
Expand All @@ -45,105 +46,105 @@ interface SikshaApi {
@Part("score") score: Long,
@Part comment: MultipartBody.Part,
@Part images: List<MultipartBody.Part>
): LeaveReviewResult
): NetworkResult<LeaveReviewResult>

@POST("/auth/login/kakao")
suspend fun loginKakao(@Header("kakao-token") kakaoToken: String): LoginOAuthResult
suspend fun loginKakao(@Header("kakao-token") kakaoToken: String): NetworkResult<LoginOAuthResult>

@POST("/auth/login/google")
suspend fun loginGoogle(@Header("google-token") googleToken: String): LoginOAuthResult
suspend fun loginGoogle(@Header("google-token") googleToken: String): NetworkResult<LoginOAuthResult>

@DELETE("/auth/")
suspend fun deleteAccount()

@POST("/auth/refresh")
suspend fun refreshToken(@Header("authorization-token") token: String): LoginOAuthResult
suspend fun refreshToken(@Header("authorization-token") token: String): NetworkResult<LoginOAuthResult>

@GET("/reviews/comments/recommendation")
suspend fun fetchRecommendationReviewComments(@Query("score") score: Long):
FetchRecommendationReviewCommentsResult
NetworkResult<FetchRecommendationReviewCommentsResult>

@GET("/reviews/dist")
suspend fun fetchReviewDistribution(@Query("menu_id") menuId: Long):
FetchReviewDistributionResult
NetworkResult<FetchReviewDistributionResult>

@POST("/voc")
suspend fun sendVoc(
@Body req: VocParam
)
): NetworkResult<Unit>

@GET("/auth/me/image")
suspend fun getUserData(): GetUserDataResult
suspend fun getUserData(): NetworkResult<GetUserDataResult>

@Multipart
@PATCH("/auth/me/image/profile")
suspend fun updateUserData(
@Part image: MultipartBody.Part?,
@Part("change_to_default_image") changeToDefaultImage: Boolean,
@Part nickname: MultipartBody.Part?
): GetUserDataResult
): NetworkResult<GetUserDataResult>

@GET("/auth/nicknames/validate")
suspend fun checkNickname(
@Query("nickname") nickname: String
)
): NetworkResult<Unit>

@GET("/versions/android")
suspend fun getVersion(): GetVersionResult
suspend fun getVersion(): NetworkResult<GetVersionResult>

@POST("/menus/{menu_id}/like")
suspend fun postLikeMenu(@Path("menu_id") menuId: Long): MenuLikeOrUnlikeResponse
suspend fun postLikeMenu(@Path("menu_id") menuId: Long): NetworkResult<MenuLikeOrUnlikeResponse>

@POST("/menus/{menu_id}/unlike")
suspend fun postUnlikeMenu(@Path("menu_id") menuId: Long): MenuLikeOrUnlikeResponse
suspend fun postUnlikeMenu(@Path("menu_id") menuId: Long): NetworkResult<MenuLikeOrUnlikeResponse>

@GET("/community/boards")
suspend fun getBoards(): GetBoardsResult
suspend fun getBoards(): NetworkResult<GetBoardsResult>

@GET("/community/boards/{board_id}")
suspend fun getBoard(
@Path("board_id") boardId: Long
): GetBoardResult
): NetworkResult<GetBoardResult>

@GET("/community/posts")
suspend fun getPosts(
@Query("board_id") boardId: Long,
@Query("page") page: Long,
@Query("per_page") perPage: Int
): GetPostsResult
): NetworkResult<GetPostsResult>

@GET("/community/posts/me")
suspend fun getUserPosts(
@Query("page") page: Long,
@Query("per_page") perPage: Int
): GetPostsResult
): NetworkResult<GetPostsResult>

@GET("/community/posts/{post_id}")
suspend fun getPost(
@Path("post_id") postId: Long
): GetPostResult
): NetworkResult<GetPostResult>

@GET("/community/comments")
suspend fun getComments(
@Query("post_id") postId: Long,
@Query("page") page: Long,
@Query("per_page") perPage: Int
): GetCommentsResult
): NetworkResult<GetCommentsResult>

@POST("/community/comments")
suspend fun postComment(
@Body body: PostCommentRequestBody
): PostCommentResponse
): NetworkResult<PostCommentResponse>

@POST("/community/posts/{post_id}/like")
suspend fun postLikePost(
@Path("post_id") postId: Long
): PostLikePostResponse
): NetworkResult<PostLikePostResponse>

@POST("/community/posts/{post_id}/unlike")
suspend fun postUnlikePost(
@Path("post_id") postId: Long
): PostUnlikePostResponse
): NetworkResult<PostUnlikePostResponse>

@Multipart
@POST("/community/posts")
Expand All @@ -153,7 +154,7 @@ interface SikshaApi {
@Part content: MultipartBody.Part,
@Part("anonymous") anonymous: Boolean,
@Part images: List<MultipartBody.Part>
): CreatePostResponse
): NetworkResult<CreatePostResponse>

@Multipart
@PATCH("/community/posts/{post_id}")
Expand All @@ -164,17 +165,17 @@ interface SikshaApi {
@Part content: MultipartBody.Part,
@Part("anonymous") anonymous: Boolean,
@Part images: List<MultipartBody.Part>
): PatchPostResponse
): NetworkResult<PatchPostResponse>

@POST("/community/comments/{comment_id}/like")
suspend fun postLikeComment(
@Path("comment_id") commentId: Long
): PostLikeCommentResponse
): NetworkResult<PostLikeCommentResponse>

@POST("/community/comments/{comment_id}/unlike")
suspend fun postUnlikeComment(
@Path("comment_id") commentId: Long
): PostUnlikeCommentResponse
): NetworkResult<PostUnlikeCommentResponse>

@DELETE("community/posts/{postId}")
suspend fun deletePost(
Expand All @@ -190,14 +191,14 @@ interface SikshaApi {
suspend fun reportPost(
@Path("post_id") postId: Long,
@Body requestBody: ReportPostRequestBody
): ReportPostResponse
): NetworkResult<ReportPostResponse>

@POST("/community/comments/{comment_id}/report")
suspend fun reportComment(
@Path("comment_id") commentId: Long,
@Body requestBody: ReportCommentRequestBody
): ReportCommentResponse
): NetworkResult<ReportCommentResponse>

@GET("/community/posts/popular/trending")
suspend fun getTrendingPosts(): GetTrendingPostsResponse
suspend fun getTrendingPosts(): NetworkResult<GetTrendingPostsResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.wafflestudio.siksha2.network.dto.core

import com.squareup.moshi.JsonClass

// TODO: error dto 형식 서버와 확정
/*@JsonClass(generateAdapter = true)
data class ErrorDto(
@Json(name = "detail") val details: List<ErrorDetail>,
val body: String?,
val message: String,
)
data class ErrorDetail(
val type: String,
val location: List<String>,
val msg: String,
val input: String?,
val url: String,
)*/

@JsonClass(generateAdapter = true)
data class ErrorDto(
val message: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.wafflestudio.siksha2.network.result

import java.io.IOException

sealed interface NetworkResult<out T : Any> {
data class Success<T : Any>(val body: T) : NetworkResult<T>
data class Failure(val message: String) : NetworkResult<Nothing>
data class NetworkError(val exception: IOException) : NetworkResult<Nothing>
data class UnknownError(val t: Throwable?) : NetworkResult<Nothing>

fun <R : Any> map(transform: (T) -> R): NetworkResult<R> = when (this) {
is Success -> Success(transform(body))
is Failure -> this
is NetworkError -> this
is UnknownError -> this
}

fun onSuccess(action: (value: T) -> Unit): NetworkResult<T> = apply {
if (this is Success) action(body)
}

fun onFailure(action: (message: String) -> Unit): NetworkResult<T> = apply {
if (this is Failure) action(message)
}

fun onNetworkError(action: (exception: IOException) -> Unit): NetworkResult<T> = apply {
if (this is NetworkError) action(exception)
}

fun onUnknownError(action: (exception: Throwable?) -> Unit): NetworkResult<T> = apply {
if (this is UnknownError) action(t)
}

fun onError(action: (t: Throwable?) -> Unit): NetworkResult<T> = apply {
when (this) {
is NetworkError -> action(exception)
is UnknownError -> action(t)
else -> Unit
}
}
}
Loading

0 comments on commit c696f8b

Please sign in to comment.