-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12eb001
commit 74e0665
Showing
16 changed files
with
113 additions
and
218 deletions.
There are no files selected for viewing
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
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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/tj/rsdevteam/inmuslim/data/exceptions/ApiException.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tj.rsdevteam.inmuslim.data.exceptions | ||
|
||
class ApiException(msg: String?) : Exception(msg) |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/tj/rsdevteam/inmuslim/data/exceptions/ConnectionTimeoutException.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tj.rsdevteam.inmuslim.data.exceptions | ||
|
||
class ConnectionTimeoutException : Exception("No internet connection") |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/tj/rsdevteam/inmuslim/data/exceptions/UnknownException.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tj.rsdevteam.inmuslim.data.exceptions | ||
|
||
class UnknownException(error: String?) : Exception("Something went wrong...\n$error") |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/tj/rsdevteam/inmuslim/data/models/network/MessageResponse.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tj.rsdevteam.inmuslim.data.models.network | ||
|
||
import com.squareup.moshi.JsonClass | ||
|
||
/** | ||
* Created by Rustam Safarov on 6/25/24. | ||
* github.com/rustamsafarovrs | ||
*/ | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class MessageResponse( | ||
val result: Int, | ||
val msg: String | ||
) |
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
100 changes: 17 additions & 83 deletions
100
app/src/main/java/tj/rsdevteam/inmuslim/data/repositories/ErrorHandler.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,32 @@ | ||
package tj.rsdevteam.inmuslim.data.repositories | ||
|
||
import com.squareup.moshi.Moshi | ||
import okhttp3.ResponseBody | ||
import retrofit2.HttpException | ||
import tj.rsdevteam.inmuslim.data.constants.Constants | ||
import tj.rsdevteam.inmuslim.data.models.network.ApiError | ||
import tj.rsdevteam.inmuslim.data.exceptions.ApiException | ||
import tj.rsdevteam.inmuslim.data.exceptions.ConnectionTimeoutException | ||
import tj.rsdevteam.inmuslim.data.exceptions.UnknownException | ||
import tj.rsdevteam.inmuslim.data.models.network.MessageResponse | ||
import tj.rsdevteam.inmuslim.data.models.network.Resource | ||
import java.io.IOException | ||
import java.net.UnknownHostException | ||
|
||
/** | ||
* Created by Rustam Safarov on 8/13/23. | ||
* github.com/rustamsafarovrs | ||
*/ | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
class ErrorHandler constructor(private val moshi: Moshi) { | ||
|
||
fun <T> getError(e: Throwable): Resource<T> { | ||
val exception = handleException(e) | ||
return getErrorResource(exception) | ||
} | ||
|
||
fun <T> getError(httpStatusCode: Int, responseBody: ResponseBody?): Resource<T> { | ||
val exception = handleHttpException(httpStatusCode, responseBody?.string()) | ||
return getErrorResource(exception) | ||
} | ||
|
||
fun <T> getError(httpStatusCode: Int, errorBody: ResponseBody?, body: Any?): Resource<T> { | ||
if (errorBody != null) { | ||
val exception = handleHttpException(httpStatusCode, errorBody.string()) | ||
return getErrorResource(exception) | ||
class ErrorHandler { | ||
|
||
fun <T : Any> getError(result: Result<*>): Resource<T> { | ||
return if (result.getOrNull() is MessageResponse) { | ||
Resource.Error(error = ApiException((result.getOrThrow() as MessageResponse).msg)) | ||
} else if (result.exceptionOrNull() is UnknownHostException) { | ||
Resource.Error(error = ConnectionTimeoutException()) | ||
} else if (result.exceptionOrNull() is HttpException) { | ||
Resource.Error(error = ApiException(result.exceptionOrNull()?.localizedMessage)) | ||
} else if (result.exceptionOrNull() is IOException) { | ||
Resource.Error(error = ConnectionTimeoutException()) | ||
} else { | ||
val tmp = moshi.adapter(Any::class.java) | ||
val str: String = tmp.toJson(body) | ||
val exception = handleHttpException(httpStatusCode, str) | ||
return getErrorResource(exception) | ||
} | ||
} | ||
|
||
private fun handleException(e: Throwable): Exception = | ||
when (e) { | ||
is HttpException -> { | ||
handleHttpException(e.code(), e.response()?.errorBody()?.string()) | ||
} | ||
|
||
is IOException -> { | ||
ConnectionTimeoutException() | ||
} | ||
|
||
else -> { | ||
e.printStackTrace() | ||
UnknownException(e.message.toString()) | ||
} | ||
Resource.Error(error = UnknownException(result.exceptionOrNull()?.localizedMessage)) | ||
} | ||
|
||
private fun handleHttpException(code: Int, errorBody: String?): Exception = | ||
try { | ||
val error: ApiError = moshi.adapter(ApiError::class.java).fromJson(errorBody!!)!! | ||
if (code == Constants.UNAUTHORIZED) { | ||
SessionException() | ||
} else { | ||
ApiException(error) | ||
} | ||
} catch (e: Exception) { | ||
// Logger.exception(e) | ||
UnknownException(e.message.toString()) | ||
} | ||
|
||
private fun <T> getErrorResource(e: Exception): Resource<T> { | ||
var message: String? = null | ||
|
||
when (e) { | ||
is ApiException -> { | ||
message = e.apiError.msg | ||
} | ||
|
||
is ConnectionTimeoutException -> { | ||
message = "No internet connection" | ||
} | ||
|
||
is UnknownException -> { | ||
message = "Something went wrong..." | ||
} | ||
|
||
is SessionException -> { | ||
message = e.message.toString() | ||
} | ||
} | ||
return Resource.error(msg = message, data = null, exception = e) | ||
|
||
} | ||
} | ||
|
||
class SessionException : Exception("Unauthorized") | ||
class UnknownException(error: String) : Exception("Something went wrong...\n$error") | ||
class ConnectionTimeoutException : Exception("No internet connection") | ||
class ApiException(val apiError: ApiError) : Exception(apiError.msg) |
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
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
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
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
Oops, something went wrong.