Skip to content

Instantly share code, notes, and snippets.

@abos3d
Created June 22, 2021 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abos3d/76df4fcbffd7e74da685549a2cb4f462 to your computer and use it in GitHub Desktop.
Save abos3d/76df4fcbffd7e74da685549a2cb4f462 to your computer and use it in GitHub Desktop.
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