better error handling

This commit is contained in:
mertalev
2026-01-23 14:55:01 -05:00
parent 538cc926da
commit 40dd94d10d
@@ -281,7 +281,7 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
) : UrlRequest.Callback() { ) : UrlRequest.Callback() {
private var buffer: NativeByteBuffer? = null private var buffer: NativeByteBuffer? = null
private var wrapped: ByteBuffer? = null private var wrapped: ByteBuffer? = null
private var httpError: IOException? = null private var error: Exception? = null
override fun onRedirectReceived(request: UrlRequest, info: UrlResponseInfo, newUrl: String) { override fun onRedirectReceived(request: UrlRequest, info: UrlResponseInfo, newUrl: String) {
request.followRedirect() request.followRedirect()
@@ -289,10 +289,11 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) { override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) {
if (info.httpStatusCode !in 200..299) { if (info.httpStatusCode !in 200..299) {
httpError = IOException("HTTP ${info.httpStatusCode}: ${info.httpStatusText}") error = IOException("HTTP ${info.httpStatusCode}: ${info.httpStatusText}")
return request.cancel() return request.cancel()
} }
try {
val contentLength = info.allHeaders["content-length"]?.firstOrNull()?.toIntOrNull() ?: 0 val contentLength = info.allHeaders["content-length"]?.firstOrNull()?.toIntOrNull() ?: 0
if (contentLength > 0) { if (contentLength > 0) {
buffer = NativeByteBuffer(contentLength + 1) buffer = NativeByteBuffer(contentLength + 1)
@@ -302,6 +303,10 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
buffer = NativeByteBuffer(INITIAL_BUFFER_SIZE) buffer = NativeByteBuffer(INITIAL_BUFFER_SIZE)
request.read(buffer!!.wrapRemaining()) request.read(buffer!!.wrapRemaining())
} }
} catch (e: Exception) {
error = e
return request.cancel()
}
} }
override fun onReadCompleted( override fun onReadCompleted(
@@ -309,6 +314,7 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
info: UrlResponseInfo, info: UrlResponseInfo,
byteBuffer: ByteBuffer byteBuffer: ByteBuffer
) { ) {
try {
val buf = if (wrapped == null) { val buf = if (wrapped == null) {
buffer!!.run { buffer!!.run {
advance(byteBuffer.position()) advance(byteBuffer.position())
@@ -319,6 +325,10 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
wrapped wrapped
} }
request.read(buf) request.read(buf)
} catch (e: Exception) {
error = e
return request.cancel()
}
} }
override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) {
@@ -335,7 +345,7 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
override fun onCanceled(request: UrlRequest, info: UrlResponseInfo?) { override fun onCanceled(request: UrlRequest, info: UrlResponseInfo?) {
buffer?.free() buffer?.free()
onFailure(httpError ?: OperationCanceledException()) onFailure(error ?: OperationCanceledException())
onComplete() onComplete()
} }
} }