1
1
package com.velogm.data.repositoryimpl
2
2
3
3
import com.velogm.data.datasource.TagDataSource
4
+ import com.velogm.domain.OutResult
4
5
import com.velogm.domain.model.PostList
5
6
import com.velogm.domain.model.Tag
6
7
import com.velogm.domain.repository.TagRepository
7
8
import kotlinx.coroutines.flow.Flow
8
9
import kotlinx.coroutines.flow.flow
10
+ import retrofit2.HttpException
11
+ import java.lang.RuntimeException
9
12
import javax.inject.Inject
10
13
11
14
class TagRepositoryImpl @Inject constructor(
12
15
private val dataSource : TagDataSource
13
16
) : TagRepository {
14
17
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 " ))
21
26
}
27
+ emit(outcome)
22
28
}
23
29
24
30
override suspend fun getPopularTag (): Flow <List <Tag >> {
@@ -56,4 +62,9 @@ class TagRepositoryImpl @Inject constructor(
56
62
emit(result.getOrDefault(PostList (emptyList())))
57
63
}
58
64
}
59
- }
65
+ }
66
+
67
+ class VelogHttpException (
68
+ val httpCode : Int ,
69
+ override val message : String ,
70
+ ) : RuntimeException()
0 commit comments