Commit 8617efc6 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

added custom error model

parent 19977c81
......@@ -82,7 +82,7 @@ const val CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID=R.string.api_
@StringRes
const val CUSTOM_API_ERROR_DEFAULT_MESSAGE_ID=R.string.api_error_default
sealed class CustomApiException(val code:Int,@StringRes val messageStringId: Int):Exception() {
sealed class CustomApiException(val code:Int,@StringRes val messageStringId: Int?, val customMessage:String? = null):Exception() {
class NotAuthorizedException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_100
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_100_MESSAGE_ID)
class TokenSyntaxException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_101
......@@ -129,24 +129,15 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In
class TourByIdNotFoundException() : CustomApiException(
CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID)
class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int)
: CustomApiException(code,messageStringId)
class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int?, apiMessage:String?)
: CustomApiException(code,messageStringId,apiMessage)
}
fun parseException(err: ErrorRaw):CustomApiException{
return when(err.code){
CUSTOM_API_ERROR_RESPONSE_CODE_100 -> CustomApiException.NotAuthorizedException()
CUSTOM_API_ERROR_RESPONSE_CODE_101 -> CustomApiException.TokenSyntaxException()
CUSTOM_API_ERROR_RESPONSE_CODE_102 -> CustomApiException.UserWithTokenNotFoundException()
CUSTOM_API_ERROR_RESPONSE_CODE_103 -> CustomApiException.TokenEstimatedException()
CUSTOM_API_ERROR_RESPONSE_CODE_104 -> CustomApiException.UserBannedException()
CUSTOM_API_ERROR_RESPONSE_CODE_111 -> CustomApiException.WrongLoginException()
CUSTOM_API_ERROR_RESPONSE_CODE_112 -> CustomApiException.WrongPasswordException()
else -> CustomApiException.UnknownCustomApiException(err.code, CUSTOM_API_ERROR_DEFAULT_MESSAGE_ID)
}
}
//as an agreement error message should be correct for user (and localized, if needed) on server-side
fun parseException(err: ErrorRaw) =
CustomApiException.UnknownCustomApiException(err.code, null, err.message)
......@@ -74,6 +74,7 @@ class AuthScreenController :
is AuthScreenViewState.Authorization -> render(viewState)
is AuthScreenViewState.SignedIn -> render(viewState)
is AuthScreenViewState.SignInError -> render(viewState)
is AuthScreenViewState.SomeError -> render(viewState)
}
}
......@@ -94,10 +95,14 @@ class AuthScreenController :
}
private fun render(viewState: AuthScreenViewState.SignInError){
snackbar.showSnackBar(viewState.message, Snackbar.LENGTH_LONG)
// toolBar.hideAll()
viewState.exception.stringId?.let { snackbar.showSnackBar(it) }
viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
signInButton.isEnabled=true
// snacky.showSnackBar("lul")
}
private fun render(viewState: AuthScreenViewState.SomeError){
viewState.exception.stringId?.let { snackbar.showSnackBar(it) }
viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
}
......
package com.biganto.visual.roompark.presentation.screen.auth
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.conductor.BigantoBasePresenter
import com.biganto.visual.roompark.data.service.network.NoNetworkException
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.interactor.AuthInteractor
import com.biganto.visual.roompark.util.monades.ExceptionString
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
......@@ -17,23 +21,47 @@ class AuthScreenPresenter @Inject constructor(
)
: BigantoBasePresenter<AuthScreen, AuthScreenViewState>() {
private fun parseError(t: Throwable): AuthScreenViewState =
when (t) {
is CustomApiException -> onCodeReturn(e = t)
is NoNetworkException -> onNoNetwork(t)
else -> onRandomError(t)
}
private fun onRandomError(t: Throwable): AuthScreenViewState =
AuthScreenViewState.SomeError(
ExceptionString(R.string.unknown_error, null)
)
private fun onNoNetwork(e: NoNetworkException): AuthScreenViewState =
AuthScreenViewState.SomeError(
ExceptionString(R.string.no_network_error, null)
)
private fun onCodeReturn(e: CustomApiException): AuthScreenViewState =
when (e.code) {
else -> AuthScreenViewState.SignInError(ExceptionString(e))
}
override fun bindIntents() {
val onAuth = intent(AuthScreen::tryAuth)
.flatMap<AuthScreenViewState>{ model ->
.flatMap<AuthScreenViewState> { model ->
interactor.signIn(model.login, model.pwd)
.doOnNext{Timber.d("auth returned $it")}
.doOnNext { Timber.d("auth returned $it") }
.map { it }
.map<AuthScreenViewState> { AuthScreenViewState.SignInError("") }
.map { AuthScreenViewState.SignedIn() }
}
// .startWith(Observable.just(AuthScreenViewState.Authorization()))
val state = restoreStateObservable
.mergeWith(onAuth)
.doOnError{ Timber.e(it)}
.doOnError { Timber.e(it) }
.onErrorReturn(::parseError)
.subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread())
......
package com.biganto.visual.roompark.presentation.screen.auth
import com.biganto.visual.roompark.conductor.BigantoBaseViewState
import com.biganto.visual.roompark.util.monades.ExceptionString
/**
* Created by Vladislav Bogdashkin on 30.09.2019.
......@@ -11,5 +12,6 @@ sealed class AuthScreenViewState : BigantoBaseViewState() {
class Idle : AuthScreenViewState()
class Authorization : AuthScreenViewState()
class SignedIn : AuthScreenViewState()
class SignInError(val message:String) : AuthScreenViewState()
class SignInError(val exception: ExceptionString) : AuthScreenViewState()
class SomeError(val exception: ExceptionString) : AuthScreenViewState()
}
\ No newline at end of file
package com.biganto.visual.roompark.util.monades
import androidx.annotation.StringRes
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
/**
* Created by Vladislav Bogdashkin on 12.11.2019.
*/
sealed class Either<out E, out V> {
data class Left<out E>(val left: E) : Either<E, Nothing>()
data class Right<out V>(val right: V) : Either<Nothing, V>()
}
data class ExceptionString(@StringRes var stringId: Int?, var message: String?) {
constructor(e:CustomApiException) : this(
if (e.message!=null) null else e.messageStringId,
if (e.messageStringId!=null) null else e.message
)
init {
assert(stringId!=null && message!=null) { "both values cannot be non-null" }
assert(stringId==null && message==null) { "both values cannot be null" }
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.util.view_utils.snackbar
import android.app.Activity
import android.view.View
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.util.view_utils.snackbar.SnackBarMessageType.*
......@@ -52,6 +53,10 @@ class SnackBarProvider @Inject constructor(val activity: Activity) : ISnackBarPr
this.rootView = rootView
}
override fun showSnackBar(@StringRes stringId: Int) {
showSnackBar(activity.resources.getString(stringId), Snackbar.LENGTH_SHORT)
}
override fun showSnackBar(message: String) {
showSnackBar(message, Snackbar.LENGTH_SHORT)
}
......@@ -78,6 +83,7 @@ interface ISnackBarProvider{
fun showSnackBar(message: String)
fun showSnackBar(message: String, length: Int)
fun showSnackBar(message: String, type: SnackBarMessageType, length: Int)
fun showSnackBar(@StringRes stringId: Int)
}
enum class SnackBarMessageType{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment