From 09d8c55306755578d8c50c3441b1948f4667de66 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 28 Aug 2024 10:47:49 -0700 Subject: [PATCH] Convert a `java.lang.OutOfMemoryError` into a `ClientException` Fixes https://github.com/dart-lang/http/issues/1271 --- pkgs/cronet_http/lib/src/cronet_client.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index f55617eeb1..28ac921f84 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -392,8 +392,19 @@ class CronetClient extends BaseClient { headers.forEach((k, v) => builder.addHeader(k.toJString(), v.toJString())); if (body.isNotEmpty) { + final JByteBuffer data; + try { + data = body.toJByteBuffer(); + } on JniException catch (e) { + if (e.message.contains('java.lang.OutOfMemoryError:')) { + throw ClientException( + 'Not enough memory for request body: ${e.message}', request.url); + } + rethrow; + } + builder.setUploadDataProvider( - jb.UploadDataProviders.create2(body.toJByteBuffer()), _executor); + jb.UploadDataProviders.create2(data), _executor); } builder.build().start(); return responseCompleter.future;