Skip to content

Commit

Permalink
Merge pull request #1366 from Adyen/fix/error_logging
Browse files Browse the repository at this point in the history
Improve error logging for analytics and image loader
  • Loading branch information
jreij authored Oct 18, 2023
2 parents 3999b6d + ce61123 commit e54b3ab
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.adyen.checkout.core.internal.data.model.ErrorResponseBody
* Indicates that an internal API call has failed.
*/
class HttpException(
val code: Int,
override val message: String,
code: Int,
message: String,
val errorBody: ErrorResponseBody?,
) : CheckoutException(message)
) : CheckoutException("$code $message")
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ internal class OkHttpClient(
response.body?.close()
return bytes
} else {
val errorBody = response.errorBody()
val exception = HttpException(response.code, response.message, errorBody)
val exception = HttpException(
code = response.code,
message = response.message,
errorBody = response.errorBody()
)
response.body?.close()
throw exception
}
Expand All @@ -98,16 +101,29 @@ internal class OkHttpClient(
(defaultHeaders + this).toHeaders()

@Suppress("SwallowedException")
private fun Response.errorBody(): ErrorResponseBody? = try {
body?.string()
?.let { JSONObject(it) }
?.let { ErrorResponseBody.SERIALIZER.deserialize(it) }
} catch (e: IOException) {
null
} catch (e: JSONException) {
null
} catch (e: ModelSerializationException) {
null
private fun Response.errorBody(): ErrorResponseBody? {
val stringBody = try {
body?.string()
} catch (e: IOException) {
null
}

val parsedErrorResponseBody = try {
stringBody
?.let { JSONObject(it) }
?.let { ErrorResponseBody.SERIALIZER.deserialize(it) }
} catch (e: JSONException) {
null
} catch (e: ModelSerializationException) {
null
}

return parsedErrorResponseBody ?: ErrorResponseBody(
status = null,
errorCode = null,
message = stringBody,
errorType = null,
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import org.json.JSONObject
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
data class ErrorResponseBody(
val status: Int,
val errorCode: String,
val message: String,
val errorType: String,
val status: Int?,
val errorCode: String?,
val message: String?,
val errorType: String?,
) : ModelObject() {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class DefaultAnalyticsRepository(
Logger.v(TAG, "Analytics setup call successful")
}.onFailure { e ->
state = State.Failed
Logger.e(TAG, "Failed to send analytics setup call", e)
// TODO change back to error when all analytic endpoints are live
Logger.w(TAG, "Failed to send analytics setup call - ${e::class.simpleName}: ${e.message}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun ImageView.load(
url,
onSuccess = { setImageBitmap(it) },
onError = { e ->
Logger.e(TAG, "Failed loading image for $url", e)
Logger.w(TAG, "Failed loading image for $url - ${e::class.simpleName}: ${e.message}")
setImageResource(errorFallback)
}
)
Expand Down

0 comments on commit e54b3ab

Please sign in to comment.