Skip to content

Instantly share code, notes, and snippets.

@abbath0767
Created July 18, 2017 10:46
Show Gist options
  • Save abbath0767/b1ee4903e3a418d4e09d96c37a9aeeb4 to your computer and use it in GitHub Desktop.
Save abbath0767/b1ee4903e3a418d4e09d96c37a9aeeb4 to your computer and use it in GitHub Desktop.
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response: Response?
try {
response = chain.proceed(request)
val responseBody = response.body()
val source = responseBody.source()
source.request(Long.MAX_VALUE)
val buffer = source.buffer()
var charset = UTF8
val contentType = responseBody.contentType()
if (contentType != null) {
charset = contentType.charset(UTF8)
}
val jsonString = buffer.clone().readString(charset)
val messageJson = JSONObject(jsonString)
val message = messageJson.getString("message")
Answers.getInstance().logCustom(CustomEvent(serverErrorTag)
.putCustomAttribute(codeKey, response?.code() ?: -1)
.putCustomAttribute(serverError.urlKey, response?.request()?.url()?.toString() ?: "empty")
.putCustomAttribute(messageKey, message))
} catch(e: Exception) {
Timber.i("intercept. error: ${e.message}, ${e.javaClass}")
Answers.getInstance().logCustom(CustomEvent(exceptionTag)
.putCustomAttribute(exceptionError.urlKey, request.url().toString())
.putCustomAttribute(exceptionName, e.javaClass.simpleName))
throw e
}
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment