Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun Throwable.toNetworkError(): Error = when (this) {
- is HttpException -> {
- val errorBody = response()?.errorBody()
- if (errorBody != null) {
- when (code()) {
- HTTP_FORBIDDEN -> Forbidden(this)
- HTTP_UNAUTHORIZED -> Unauthorized(this)
- else -> Http(code(), response()?.message(), this)
- }
- } else Http(code(), response()?.message(), this)
- }
- is IOException -> Connection(this)
- else -> GenericError(cause = this)
- }
- ...
- sealed class NetworkError(override val cause: Throwable) : Error(cause) {
- data class Http(val code: Int, override val message: String?, override val cause: Throwable) :
- NetworkError(cause)
- data class Forbidden(override val cause: Throwable) : NetworkError(cause)
- data class Unauthorized(override val cause: Throwable) : NetworkError(cause)
- data class Connection(override val cause: Throwable) : NetworkError(cause)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement