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,18 +289,23 @@ 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()
} }
val contentLength = info.allHeaders["content-length"]?.firstOrNull()?.toIntOrNull() ?: 0 try {
if (contentLength > 0) { val contentLength = info.allHeaders["content-length"]?.firstOrNull()?.toIntOrNull() ?: 0
buffer = NativeByteBuffer(contentLength + 1) if (contentLength > 0) {
wrapped = NativeBuffer.wrap(buffer!!.pointer, contentLength + 1) buffer = NativeByteBuffer(contentLength + 1)
request.read(wrapped) wrapped = NativeBuffer.wrap(buffer!!.pointer, contentLength + 1)
} else { request.read(wrapped)
buffer = NativeByteBuffer(INITIAL_BUFFER_SIZE) } else {
request.read(buffer!!.wrapRemaining()) buffer = NativeByteBuffer(INITIAL_BUFFER_SIZE)
request.read(buffer!!.wrapRemaining())
}
} catch (e: Exception) {
error = e
return request.cancel()
} }
} }
@@ -309,16 +314,21 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
info: UrlResponseInfo, info: UrlResponseInfo,
byteBuffer: ByteBuffer byteBuffer: ByteBuffer
) { ) {
val buf = if (wrapped == null) { try {
buffer!!.run { val buf = if (wrapped == null) {
advance(byteBuffer.position()) buffer!!.run {
ensureHeadroom() advance(byteBuffer.position())
wrapRemaining() ensureHeadroom()
wrapRemaining()
}
} else {
wrapped
} }
} else { request.read(buf)
wrapped } catch (e: Exception) {
error = e
return request.cancel()
} }
request.read(buf)
} }
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()
} }
} }