Skip to content

Commit

Permalink
fix: Log the exception class name with the ApiException logging and m…
Browse files Browse the repository at this point in the history
…ove the Retrofit builder to the RetrofitConfiguration
  • Loading branch information
ShivamNagpal committed Mar 25, 2023
1 parent 3c322a1 commit cb7283f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ package com.nagpal.shivam.vtucslab.retrofit

import android.util.Log
import com.nagpal.shivam.vtucslab.retrofit.ApiResult.*
import com.nagpal.shivam.vtucslab.utilities.StaticMethods
import retrofit2.HttpException
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory


fun getRetrofitBuilder(): Retrofit.Builder {
return Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create(StaticMethods.jsonMapper))
.addCallAdapterFactory(ApiResultCallAdapterFactory.create())
}

fun <T : Any> handleApiResult(
execute: () -> Response<T>
Expand Down Expand Up @@ -39,5 +51,9 @@ fun <T : Any> logNetworkResultException(
url: String,
networkResult: ApiException<T>
) {
Log.e(logTag, "Call to $url failed with exception", networkResult.throwable)
Log.e(
logTag,
"Call to $url failed with exception: ${networkResult.throwable.javaClass.name}",
networkResult.throwable
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nagpal.shivam.vtucslab.services
import com.nagpal.shivam.vtucslab.models.LaboratoryExperimentResponse
import com.nagpal.shivam.vtucslab.models.LaboratoryResponse
import com.nagpal.shivam.vtucslab.retrofit.ApiResult
import com.nagpal.shivam.vtucslab.retrofit.getRetrofitBuilder
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.StaticMethods
import retrofit2.http.GET
Expand All @@ -21,7 +22,7 @@ interface VtuCsLabService {

companion object {
val instance: VtuCsLabService by lazy {
val retrofit = StaticMethods.getRetrofitBuilder()
val retrofit = getRetrofitBuilder()
.baseUrl(Constants.GITHUB_RAW_BASE_URL)
.build()
return@lazy retrofit.create(VtuCsLabService::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.nagpal.shivam.vtucslab.models.LaboratoryExperimentResponse
import com.nagpal.shivam.vtucslab.models.LaboratoryResponse
import com.nagpal.shivam.vtucslab.retrofit.ApiResultCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory

object StaticMethods {

private val jsonMapper: JsonMapper by lazy {
val jsonMapper: JsonMapper by lazy {
com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build()
Expand All @@ -21,13 +17,6 @@ object StaticMethods {
return programName.replace('_', ' ')
}

fun getRetrofitBuilder(): Retrofit.Builder {
return Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create(jsonMapper))
.addCallAdapterFactory(ApiResultCallAdapterFactory.create())
}

fun getBaseURL(labResponse: LaboratoryResponse): String {
return "${labResponse.githubRawContent}/${labResponse.organization}/${labResponse.repository}/${labResponse.branch}"
}
Expand Down

0 comments on commit cb7283f

Please sign in to comment.