This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Repository(val api: Api) { | |
fun login(email: String, password: String): Single<User> { | |
return api.login(email, password) | |
.onErrorResumeNext { error: Throwable -> | |
val exception = when (error) { | |
is HttpException -> { | |
when (error.code()) { | |
401 -> InvalidCredentialsException() | |
400 -> { | |
val errorResponse: ValidationErrorResponse = error.bodyToValidationError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoInternetConnectionException() : Exception() | |
class GeneralServerException(val code: Int) : Exception() | |
class UnexpectedException(val originalException: Exception) : Exception() | |
class InvalidCredentialsException : Exception() | |
class ValidationException(val errors: Map<String, List<String>>) : Exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 400 error body entities | |
class FieldError(val field: String, val error: String) | |
class ValidationErrorResponse(val fields: List<FieldError>) | |
// repository reading data from api | |
class Repository(val api: Api) { | |
fun login(email: String, password: String): Single<User> = api.login(email, password) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"fields": [ | |
{ | |
"field": "email", | |
"error": "invalid_email" | |
} | |
] | |
} |
NewerOlder