From fcc7803a256a901c7e5948ae044aa99554177144 Mon Sep 17 00:00:00 2001 From: angai Date: Mon, 19 Oct 2020 21:49:15 +0300 Subject: [PATCH] onErrorMapper --- .../chucker/api/GrpcChuckerInterceptor.kt | 7 +++++-- .../com/chuckerteam/chucker/api/OnErrorMapper.kt | 14 ++++++++++++++ .../ui/transaction/TransactionPayloadFragment.kt | 12 +++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 library/src/main/java/com/chuckerteam/chucker/api/OnErrorMapper.kt diff --git a/library/src/main/java/com/chuckerteam/chucker/api/GrpcChuckerInterceptor.kt b/library/src/main/java/com/chuckerteam/chucker/api/GrpcChuckerInterceptor.kt index 4fa4582..ee64d86 100644 --- a/library/src/main/java/com/chuckerteam/chucker/api/GrpcChuckerInterceptor.kt +++ b/library/src/main/java/com/chuckerteam/chucker/api/GrpcChuckerInterceptor.kt @@ -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 interceptCall( @@ -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) } diff --git a/library/src/main/java/com/chuckerteam/chucker/api/OnErrorMapper.kt b/library/src/main/java/com/chuckerteam/chucker/api/OnErrorMapper.kt new file mode 100644 index 0000000..c64eb7b --- /dev/null +++ b/library/src/main/java/com/chuckerteam/chucker/api/OnErrorMapper.kt @@ -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 + +} \ No newline at end of file diff --git a/library/src/main/java/com/chuckerteam/chucker/internal/ui/transaction/TransactionPayloadFragment.kt b/library/src/main/java/com/chuckerteam/chucker/internal/ui/transaction/TransactionPayloadFragment.kt index baeba11..d35c54b 100644 --- a/library/src/main/java/com/chuckerteam/chucker/internal/ui/transaction/TransactionPayloadFragment.kt +++ b/library/src/main/java/com/chuckerteam/chucker/internal/ui/transaction/TransactionPayloadFragment.kt @@ -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 @@ -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