Skip to content

Commit

Permalink
onErrorMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Angainor17 committed Oct 19, 2020
1 parent 717817c commit fcc7803
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class GrpcChuckerInterceptor(
private val port: Int = 0,
private val context: Context,
private val collector: ChuckerCollector = ChuckerCollector(context),
private val onErrorMapper: OnErrorMapper? = null,
) : ClientInterceptor {

override fun <M, R> interceptCall(
Expand Down Expand Up @@ -87,13 +88,15 @@ public class GrpcChuckerInterceptor(
}

override fun onClose(status: Status, metadata: Metadata) {
val errorMessage = onErrorMapper?.mapErrorMessageToString(status, metadata) ?: status.cause?.toString()

transaction.apply {
responseCode = status.code.name
error = status.cause?.toString()

error = (if (status != Status.OK) errorMessage else status.cause?.toString())
responseDate = System.currentTimeMillis()
tookMs = (responseDate ?: 0L) - (requestDate ?: 0L)
}

responseListener.onClose(status, metadata)
collector.onResponseReceived(transaction)
}
Expand Down
14 changes: 14 additions & 0 deletions library/src/main/java/com/chuckerteam/chucker/api/OnErrorMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.chuckerteam.chucker.api

import io.grpc.Metadata
import io.grpc.Status

/**
* Обработка ошибки исходя из переменных Status и Metadata
* Необходимо из-за того, что модель ошибки индивидуальна для конкретного API
*/
public interface OnErrorMapper {

public fun mapErrorMessageToString(status: Status, metadata: Metadata): String

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.databinding.ChuckerFragmentTransactionPayloadBinding
import com.chuckerteam.chucker.internal.data.entity.Transaction
import com.chuckerteam.chucker.internal.support.combineLatest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private const val GET_FILE_FOR_SAVING_REQUEST_CODE: Int = 43

Expand Down Expand Up @@ -231,6 +231,12 @@ internal class TransactionPayloadFragment :
bodyString.lines().forEach {
result.add(TransactionPayloadItem.BodyLineItem(SpannableStringBuilder.valueOf(it)))
}
} else {
if (!transaction.error.isNullOrEmpty()) {
transaction.error.toString().lines().forEach {
result.add(TransactionPayloadItem.BodyLineItem(SpannableStringBuilder.valueOf(it)))
}
}
}

return@withContext result
Expand Down

0 comments on commit fcc7803

Please sign in to comment.