Skip to content

Commit

Permalink
Opencage geocoder should log full response on error
Browse files Browse the repository at this point in the history
  • Loading branch information
growse committed Jan 25, 2022
1 parent 8cd6bd4 commit fff1923
Showing 1 changed file with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,65 +50,66 @@ class OpenCageGeocoder @JvmOverloads internal constructor(
return try {
httpClient.newCall(request).execute().use { response ->
val responseBody = response.body?.string()
when (response.code) {
200 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.d("Opencage HTTP response: %s", responseBody)
return deserializedOpenCageResponse.formatted?.let {
GeocodeResult.Formatted(
it
)
try {
when (response.code) {
200 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.d("Opencage HTTP response: %s", responseBody)
return deserializedOpenCageResponse.formatted?.let {
GeocodeResult.Formatted(it)
} ?: GeocodeResult.Empty
}
?: GeocodeResult.Empty
}
401 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
GeocodeResult.Error(
deserializedOpenCageResponse.status?.message
?: "No error message provided", tripResetTimestamp
)
}
402 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.d("Opencage HTTP response: %s", responseBody)
Timber.w("Opencage quota exceeded")
deserializedOpenCageResponse.rate?.let { rate ->
Timber.w("Not retrying Opencage requests until ${rate.reset}")
tripResetTimestamp = rate.reset
401 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
GeocodeResult.Error(
deserializedOpenCageResponse.status?.message
?: "No error message provided", tripResetTimestamp
)
}
GeocodeResult.RateLimited(tripResetTimestamp)
}
403 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.e(responseBody)
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
if (deserializedOpenCageResponse.status?.message == "IP address rejected") {
GeocodeResult.IPAddressRejected(tripResetTimestamp)
} else {
GeocodeResult.Disabled(tripResetTimestamp)
402 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.d("Opencage HTTP response: %s", responseBody)
Timber.w("Opencage quota exceeded")
deserializedOpenCageResponse.rate?.let { rate ->
Timber.w("Not retrying Opencage requests until ${rate.reset}")
tripResetTimestamp = rate.reset
}
GeocodeResult.RateLimited(tripResetTimestamp)
}
403 -> {
val deserializedOpenCageResponse =
jsonMapper.readValue(responseBody, OpenCageResponse::class.java)
Timber.e(responseBody)
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
if (deserializedOpenCageResponse.status?.message == "IP address rejected") {
GeocodeResult.IPAddressRejected(tripResetTimestamp)
} else {
GeocodeResult.Disabled(tripResetTimestamp)
}

}
429 -> {
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
GeocodeResult.RateLimited(tripResetTimestamp)
}
else -> {
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
Timber.e("Unexpected response from Opencage: %s", response)
GeocodeResult.Error(
"status: ${response.code} $responseBody",
tripResetTimestamp
)
}
}
429 -> {
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
GeocodeResult.RateLimited(tripResetTimestamp)
}
else -> {
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
Timber.e("Unexpected response from Opencage: %s", response)
GeocodeResult.Error(
"status: ${response.code} $responseBody",
tripResetTimestamp
)
}
} catch (e: Exception) {
Timber.d("Json response: {}", responseBody)
throw e
}
}

} catch (e: Exception) {
tripResetTimestamp = Instant.now().plus(1, ChronoUnit.MINUTES)
Timber.e(e, "Error reverse geocoding from opencage")
Expand Down

0 comments on commit fff1923

Please sign in to comment.