@@ -2,8 +2,14 @@ package com.omega_r.base.errors
22
33import kotlinx.coroutines.CoroutineExceptionHandler
44import retrofit2.HttpException
5+ import retrofit2.Invocation
56import java.net.ConnectException
6- import java.net.HttpURLConnection.*
7+ import java.net.HttpURLConnection.HTTP_BAD_REQUEST
8+ import java.net.HttpURLConnection.HTTP_FORBIDDEN
9+ import java.net.HttpURLConnection.HTTP_INTERNAL_ERROR
10+ import java.net.HttpURLConnection.HTTP_NOT_FOUND
11+ import java.net.HttpURLConnection.HTTP_UNAUTHORIZED
12+ import java.net.HttpURLConnection.HTTP_UNAVAILABLE
713import java.net.SocketTimeoutException
814import java.net.UnknownHostException
915import kotlin.coroutines.CoroutineContext
@@ -16,19 +22,24 @@ open class ErrorHandler : (Throwable) -> Exception, CoroutineExceptionHandler {
1622 override val key: CoroutineContext .Key <* >
1723 get() = CoroutineExceptionHandler
1824
19- protected open fun handleHttpException (httpException : HttpException ): AppException {
25+ protected open fun handleHttpException (httpException : HttpException , method : String? ): AppException {
2026 val response = httpException.response()
2127
2228 when (response?.code()) {
2329 HTTP_INTERNAL_ERROR -> return AppException .ServerProblem (
24- " Internal server error" ,
30+ " Internal server error\n $method " ,
2531 httpException
2632 )
27- HTTP_BAD_REQUEST -> return AppException .ServerProblem (" Bad request" , httpException)
28- HTTP_UNAVAILABLE -> return AppException .ServerProblem (" Service Unavailable" , httpException)
29- HTTP_NOT_FOUND -> return AppException .NotFound (" Not found" , httpException)
30- HTTP_FORBIDDEN -> return AppException .AccessDenied (" Forbidden" , httpException)
31- HTTP_UNAUTHORIZED -> return AppException .NotAuthorized (" Unauthorized" , httpException)
33+ HTTP_BAD_REQUEST -> return AppException .ServerProblem (" Bad request\n " +
34+ " $method " , httpException)
35+ HTTP_UNAVAILABLE -> return AppException .ServerProblem (" Service Unavailable\n " +
36+ " $method " , httpException)
37+ HTTP_NOT_FOUND -> return AppException .NotFound (" Not found\n " +
38+ " $method " , httpException)
39+ HTTP_FORBIDDEN -> return AppException .AccessDenied (" Forbidden\n " +
40+ " $method " , httpException)
41+ HTTP_UNAUTHORIZED -> return AppException .NotAuthorized (" Unauthorized\n " +
42+ " $method " , httpException)
3243 }
3344
3445 return AppException .UnknownError (" Unknown error type" , httpException)
@@ -39,12 +50,27 @@ open class ErrorHandler : (Throwable) -> Exception, CoroutineExceptionHandler {
3950 is UnknownHostException -> AppException .NoConnection (null , throwable)
4051 is ConnectException ,
4152 is SocketTimeoutException -> AppException .ServerUnavailable (null , throwable)
42- is HttpException -> handleHttpException(throwable)
53+ is HttpException -> {
54+ handleHttpException(throwable, throwable.getMethod())
55+ }
4356 is AppException -> throwable
4457 else -> AppException .UnknownError (" Unknown error" , throwable)
4558 }
4659 }
4760
61+ protected open fun HttpException.getMethod (): String? {
62+ return response()?.raw()?.request()?.let {
63+ var result = " @" + it.method() + " (" + it.url().url().toString() + " ) "
64+
65+ it.tag(Invocation ::class .java)?.let { tag ->
66+ val arguments = tag.arguments().joinToString()
67+ result + = tag.method().declaringClass.simpleName + " ." + tag.method().name + " (" + arguments + " )"
68+ }
69+
70+ result
71+ }
72+ }
73+
4874 override fun handleException (context : CoroutineContext , exception : Throwable ) {
4975 throw handleThrowable(exception)
5076 }
0 commit comments