11package com.velogm.data.repositoryimpl
22
33import com.velogm.data.datasource.TagDataSource
4+ import com.velogm.domain.OutResult
45import com.velogm.domain.model.PostList
56import com.velogm.domain.model.Tag
67import com.velogm.domain.repository.TagRepository
78import kotlinx.coroutines.flow.Flow
89import kotlinx.coroutines.flow.flow
10+ import retrofit2.HttpException
11+ import java.lang.RuntimeException
912import javax.inject.Inject
1013
1114class TagRepositoryImpl @Inject constructor(
1215 private val dataSource : TagDataSource
1316) : TagRepository {
1417
15- override suspend fun getTag (): Flow <List <Tag >> {
16- return flow {
17- val result = kotlin.runCatching {
18- dataSource.getTag().map { Tag (it) }
19- }
20- emit(result.getOrDefault(emptyList()))
18+ override suspend fun getTag (): Flow <OutResult <List <Tag >>> = flow {
19+ val result = runCatching {
20+ val tag = dataSource.getTag().map { Tag (it) }
21+ OutResult .Success (tag)
22+ }
23+ val outcome = result.getOrElse {
24+ val errorCode = (it as ? HttpException )?.code() ? : - 1
25+ OutResult .Failure (error = VelogHttpException (errorCode, " $errorCode " ))
2126 }
27+ emit(outcome)
2228 }
2329
2430 override suspend fun getPopularTag (): Flow <List <Tag >> {
@@ -56,4 +62,9 @@ class TagRepositoryImpl @Inject constructor(
5662 emit(result.getOrDefault(PostList (emptyList())))
5763 }
5864 }
59- }
65+ }
66+
67+ class VelogHttpException (
68+ val httpCode : Int ,
69+ override val message : String ,
70+ ) : RuntimeException()
0 commit comments