Created
June 22, 2021 09:17
-
-
Save abos3d/76df4fcbffd7e74da685549a2cb4f462 to your computer and use it in GitHub Desktop.
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
package com.cleanarchitectkotlinflowhiltsimplestway.presentation | |
import org.json.JSONObject | |
import retrofit2.HttpException | |
open class NetworkErrorException( | |
val errorCode: Int = -1, | |
val errorMessage: String, | |
val response: String = "" | |
) : Exception() { | |
override val message: String | |
get() = localizedMessage | |
override fun getLocalizedMessage(): String { | |
return errorMessage | |
} | |
companion object { | |
fun parseException(e: HttpException): NetworkErrorException { | |
val errorBody = e.response()?.errorBody()?.string() | |
return try {//here you can parse the error body that comes from server | |
NetworkErrorException(e.code(), JSONObject(errorBody!!).getString("message")) | |
} catch (_: Exception) { | |
NetworkErrorException(e.code(), "unexpected error!!ً") | |
} | |
} | |
} | |
} | |
class AuthenticationException(authMessage: String) : | |
NetworkErrorException(errorMessage = authMessage) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment