Move auth response handling to runCatching
runCatching is easier to read and understand than mapCatching
This commit is contained in:
@@ -30,17 +30,18 @@ class AuthDataSourceImpl @Inject constructor(
|
|||||||
val authService = retrofitBuilder.buildRetrofit(baseUrl).create<AuthService>()
|
val authService = retrofitBuilder.buildRetrofit(baseUrl).create<AuthService>()
|
||||||
|
|
||||||
val accessToken = runCatching {
|
val accessToken = runCatching {
|
||||||
authService.getToken(username, password)
|
val response = authService.getToken(username, password)
|
||||||
}.mapCatching {
|
Timber.d("authenticate() response is $response")
|
||||||
Timber.d("authenticate() response is $it")
|
if (response.isSuccessful) {
|
||||||
if (!it.isSuccessful) {
|
checkNotNull(response.body()).accessToken
|
||||||
val cause = HttpException(it)
|
} else {
|
||||||
throw when (it.decodeErrorBodyOrNull<GetTokenResponse, ErrorDetail>()?.detail) {
|
val cause = HttpException(response)
|
||||||
|
val errorDetail: ErrorDetail? = response.decodeErrorBodyOrNull(json)
|
||||||
|
throw when (errorDetail?.detail) {
|
||||||
"Unauthorized" -> Unauthorized(cause)
|
"Unauthorized" -> Unauthorized(cause)
|
||||||
else -> NotMealie(cause)
|
else -> NotMealie(cause)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkNotNull(it.body()).accessToken // Can't be null here, would throw SerializationException
|
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
Timber.e(it, "authenticate: getToken failed")
|
Timber.e(it, "authenticate: getToken failed")
|
||||||
throw when (it) {
|
throw when (it) {
|
||||||
|
|||||||
Reference in New Issue
Block a user