Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.97 KB | None | 0 0
  1. fun Throwable.toNetworkError(): Error = when (this) {
  2.     is HttpException -> {
  3.         val errorBody = response()?.errorBody()
  4.         if (errorBody != null) {
  5.             when (code()) {
  6.                 HTTP_FORBIDDEN -> Forbidden(this)
  7.                 HTTP_UNAUTHORIZED -> Unauthorized(this)
  8.                 else -> Http(code(), response()?.message(), this)
  9.             }
  10.         } else Http(code(), response()?.message(), this)
  11.     }
  12.     is IOException -> Connection(this)
  13.     else -> GenericError(cause = this)
  14. }
  15.  
  16. ...
  17.  
  18. sealed class NetworkError(override val cause: Throwable) : Error(cause) {
  19.     data class Http(val code: Int, override val message: String?, override val cause: Throwable) :
  20.         NetworkError(cause)
  21.  
  22.     data class Forbidden(override val cause: Throwable) : NetworkError(cause)
  23.     data class Unauthorized(override val cause: Throwable) : NetworkError(cause)
  24.     data class Connection(override val cause: Throwable) : NetworkError(cause)
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement