Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error logging for analytics and image loader #1366

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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