TP-12345 | added try catch for save api log to handle crash

This commit is contained in:
AMAN SINGH
2023-04-28 20:47:06 +05:30
parent 0970f455cf
commit d204d3b653

View File

@@ -657,48 +657,57 @@ object AlfredManager {
fun saveApiLog(request: Request, response: Response, startTime: Long, endTime: Long) {
coroutineDispatcher.executor.execute {
var bytesSent = 0L
var byteReceived = 0L
request.body?.let { requestBody ->
val buffer = Buffer()
requestBody.writeTo(buffer)
bytesSent = buffer.size
}
response.body?.let { responseBody ->
val source = responseBody.source()
source.request(Long.MAX_VALUE)
val buffer = source.buffer()
byteReceived = buffer.size
}
var errorMessage: String? = null
val duration: Long = endTime - startTime
val errorType: String? = null
if (response.code != CODE_API_SUCCESS) {
errorMessage = response.message
}
val attributes = hashMapOf<String, Any>().apply {
this[AlfredConstants.URL] = request.url.toString()
this[AlfredConstants.METHOD] = request.method
this[AlfredConstants.RESPONSE_CODE] = response.code
this[AlfredConstants.ERROR_MESSAGE] = errorMessage.toString()
this[AlfredConstants.ERROR_TYPE] = errorType.toString()
this[AlfredConstants.START_TIME] = startTime
this[AlfredConstants.END_TIME] = endTime
this[AlfredConstants.DURATION_IN_MS] = duration.toDouble()
this[AlfredConstants.BYTES_RECEIVED] = byteReceived
this[AlfredConstants.BYTES_SENT] = bytesSent
}
val appPerformanceEvent = buildAppPerformanceEvent(
AlfredConstants.API_METRIC_EVENT_NAME, API_METRICS, attributes
)
AlfredDispatcher.addTaskToQueue(
AddMetricTask(
appPerformanceEvent, applicationContext
if (config.getAlfredSessionId().isNotEmpty()) {
var bytesSent = 0L
var byteReceived = 0L
if (request.body != null) {
val buffer = Buffer()
request.body?.writeTo(buffer)
bytesSent = buffer.size
}
try {
if (response.body != null) {
val responseBody = response.body
val source = responseBody?.source()
source?.request(Long.MAX_VALUE)
val buffer = source?.buffer
byteReceived = buffer?.size ?: 0
}
} catch (e: Exception) {
byteReceived = -1
e.log()
}
var errorMessage: String? = null
val duration: Long = endTime - startTime
val errorType: String? = null
if (response.code != CODE_API_SUCCESS) {
errorMessage = response.message
}
val attributes = hashMapOf<String, Any>().apply {
this[AlfredConstants.URL] = request.url.toString()
this[AlfredConstants.METHOD] = request.method
this[AlfredConstants.RESPONSE_CODE] = response.code
this[AlfredConstants.ERROR_MESSAGE] = errorMessage.toString()
this[AlfredConstants.ERROR_TYPE] = errorType.toString()
this[AlfredConstants.START_TIME] = startTime
this[AlfredConstants.END_TIME] = endTime
this[AlfredConstants.DURATION_IN_MS] = duration.toDouble()
this[AlfredConstants.BYTES_RECEIVED] = byteReceived
this[AlfredConstants.BYTES_SENT] = bytesSent
}
val appPerformanceEvent = buildAppPerformanceEvent(
AlfredConstants.API_METRIC_EVENT_NAME, API_METRICS, attributes
)
)
AlfredDispatcher.addTaskToQueue(
AddMetricTask(
appPerformanceEvent, applicationContext
)
)
}
}
}
fun stopRecording(appBackgroundView: View) {
isAppInBackground = true
hasRecordingStarted = false