Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.15 KB | None | 0 0
  1. class ErrorsInterceptor @Inject constructor(
  2.     private val networkAvailabilityProvider: NetworkAvailabilityProvider
  3. ) : Interceptor {
  4.  
  5.     private val gson = Gson()
  6.  
  7.     private var unauthorizedErrorCallback: (() -> Unit)? = null
  8.  
  9.     override fun intercept(chain: Interceptor.Chain): Response {
  10.         if (!networkAvailabilityProvider.isNetworkAvailable()) {
  11.             throw NetworkUnavailableException
  12.         }
  13.         val response = chain.proceed(chain.request())
  14.         val traceId: String? = response.header("trace-id")
  15.  
  16.         when {
  17.             response.code() in arrayOf(HTTP_UNAUTHORIZED, HTTP_FORBIDDEN) -> {
  18.                 unauthorizedErrorCallback?.invoke()
  19.                 throw UnauthorizedException("[ErrorsInterceptor]: code: ${response.code()}")
  20.             }
  21.             response.code() >= HTTP_INTERNAL_ERROR -> {
  22.                 throw BaseServerException(
  23.                     code = response.code(),
  24.                     msg = response.message(),
  25.                     traceId = traceId,
  26.                     requestInfo = response.request().toString()
  27.                 )
  28.             }
  29.             !response.isSuccessful -> {
  30.                 val parsedResponse = parseResponse(response)
  31.  
  32.                 throw BaseServerException(
  33.                     code = parsedResponse.status.code,
  34.                     msg = parsedResponse.status.message,
  35.                     desc = parsedResponse.status.description,
  36.                     traceId = traceId,
  37.                     requestInfo = response.request().toString()
  38.                 )
  39.             }
  40.             else -> {
  41.                 val parsedResponse = parseResponse(response)
  42.  
  43.                 parsedResponse
  44.                     .errors
  45.                     ?.firstOrNull()
  46.                     ?.run {
  47.                         throw ApiErrorException(code, level, message)
  48.                     }
  49.  
  50.                 // while we are using keycloak authorization, we are forced to skip this check
  51.                 // if (parsedResponse.data == null) {
  52.                 // throw NoResponseDataProvided()
  53.                 // }
  54.             }
  55.         }
  56.  
  57.         return response
  58.     }
  59.  
  60.     private fun parseResponse(response: Response): ResponseObject<*> {
  61.         try {
  62.             val responseBodyCopy = response.peekBody(Long.MAX_VALUE)
  63.             return gson.fromJson(responseBodyCopy.string(), ResponseObject::class.java)
  64.                 ?: throw KotlinNullPointerException()
  65.         } catch (ex: Throwable) {
  66.             if (ex is UnauthorizedException || response.request().url().toString().contains("redirect_uri")) {
  67.                 // todo: this is temporary solution. If token has expired, we, as response, receive redirect page, instead of error
  68.                 unauthorizedErrorCallback?.invoke()
  69.                 throw UnauthorizedException("[ErrorsInterceptor]: When trying parse response")
  70.             } else {
  71.                 throw ex
  72.             }
  73.         }
  74.     }
  75.  
  76.     fun setUnauthorizedErrorCallback(callback: () -> Unit) {
  77.         unauthorizedErrorCallback = callback
  78.     }
  79.  
  80.     fun removeUnauthorizedErrorCallback() {
  81.         unauthorizedErrorCallback = null
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement