From e86693d50a8683e0c25176ca5d4fe7dd16fd2b6e Mon Sep 17 00:00:00 2001 From: MohamedSabthar Date: Sat, 20 Jul 2024 12:29:43 +0530 Subject: [PATCH 1/3] Handle Ballerina errors when calling byte stream next method in native side --- .../stdlib/mime/util/EntityBodyHandler.java | 31 ++++++++++++++++--- .../ballerina/stdlib/mime/util/MimeUtil.java | 9 ++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java b/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java index 09cc6569..318b6db8 100644 --- a/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java +++ b/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java @@ -64,6 +64,7 @@ import static io.ballerina.stdlib.mime.util.MimeConstants.MULTIPART_AS_PRIMARY_TYPE; import static io.ballerina.stdlib.mime.util.MimeConstants.TEXT_EVENT_STREAM; import static io.ballerina.stdlib.mime.util.MimeUtil.isNotNullAndEmpty; +import static io.ballerina.stdlib.mime.util.MimeUtil.removeJavaExceptionPrefix; /** * Entity body related operations are included here. @@ -397,7 +398,18 @@ public void notifySuccess(Object result) { latch.countDown(); return; } - writeContentPart((BMap) result, outputStream); + if (result instanceof BError error) { + entity.addNativeData(ENTITY_BYTE_STREAM, null); + EntityBodyHandler.closeMessageOutputStream(outputStream); + throw error; + } + try { + writeContentPart((BMap) result, outputStream); + } catch (Exception e) { + BError error = ErrorCreator.createError(StringUtils.fromString( + removeJavaExceptionPrefix(e.getMessage()))); + this.notifyFailure(error); + } writeContent(env, entity, outputStream, iteratorObj, latch); } @@ -407,7 +419,7 @@ public void notifyFailure(BError bError) { throw ErrorCreator.createError(StringUtils.fromString( "Error occurred while streaming content: " + bError.getMessage())); } - }, null, null); + }, null, null, new Object[]{}); } /** @@ -435,7 +447,18 @@ public void notifySuccess(Object result) { EntityBodyHandler.closeMessageOutputStream(outputStream); return; } - writeContentPart((BMap) result, outputStream); + if (result instanceof BError error) { + entity.addNativeData(ENTITY_BYTE_STREAM, null); + EntityBodyHandler.closeMessageOutputStream(outputStream); + throw error; + } + try { + writeContentPart((BMap) result, outputStream); + } catch (Exception e) { + BError error = ErrorCreator.createError(StringUtils.fromString( + removeJavaExceptionPrefix(e.getMessage()))); + this.notifyFailure(error); + } writeEvent(env, entity, outputStream, iteratorObj); } @@ -453,7 +476,7 @@ private static void closeMessageOutputStream(OutputStream messageOutputStream) { if (messageOutputStream != null) { messageOutputStream.close(); } - } catch (IOException e) { + } catch (Exception e) { log.error("Couldn't close message output stream", e); } } diff --git a/native/src/main/java/io/ballerina/stdlib/mime/util/MimeUtil.java b/native/src/main/java/io/ballerina/stdlib/mime/util/MimeUtil.java index 5b94058d..2771dc96 100644 --- a/native/src/main/java/io/ballerina/stdlib/mime/util/MimeUtil.java +++ b/native/src/main/java/io/ballerina/stdlib/mime/util/MimeUtil.java @@ -47,6 +47,8 @@ import java.nio.charset.StandardCharsets; import java.util.Enumeration; import java.util.Random; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static io.ballerina.stdlib.mime.util.MimeConstants.ASSIGNMENT; import static io.ballerina.stdlib.mime.util.MimeConstants.BODY_PARTS; @@ -547,4 +549,11 @@ public static Module getMimePackage() { } private MimeUtil() {} + + public static String removeJavaExceptionPrefix(String errorMessage) { + String prefixRegex = "^(?:[a-z]+\\.)+[A-Za-z]+Exception:"; + Pattern pattern = Pattern.compile(prefixRegex); + Matcher matcher = pattern.matcher(errorMessage); + return matcher.find() ? matcher.replaceFirst("").trim() : errorMessage; + } } From b4a3de60a9fb06991575b155f3ed1d34d0121599 Mon Sep 17 00:00:00 2001 From: MohamedSabthar Date: Wed, 24 Jul 2024 10:44:10 +0530 Subject: [PATCH 2/3] Address review suggestions --- .../stdlib/mime/util/EntityBodyHandler.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java b/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java index 318b6db8..1369a29e 100644 --- a/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java +++ b/native/src/main/java/io/ballerina/stdlib/mime/util/EntityBodyHandler.java @@ -400,15 +400,15 @@ public void notifySuccess(Object result) { } if (result instanceof BError error) { entity.addNativeData(ENTITY_BYTE_STREAM, null); - EntityBodyHandler.closeMessageOutputStream(outputStream); - throw error; + this.notifyFailure(error); } try { writeContentPart((BMap) result, outputStream); } catch (Exception e) { - BError error = ErrorCreator.createError(StringUtils.fromString( - removeJavaExceptionPrefix(e.getMessage()))); - this.notifyFailure(error); + latch.countDown(); + throw ErrorCreator.createError(StringUtils.fromString( + "Error occurred while writing the stream content: " + + removeJavaExceptionPrefix(e.getMessage()))); } writeContent(env, entity, outputStream, iteratorObj, latch); } @@ -449,15 +449,15 @@ public void notifySuccess(Object result) { } if (result instanceof BError error) { entity.addNativeData(ENTITY_BYTE_STREAM, null); - EntityBodyHandler.closeMessageOutputStream(outputStream); - throw error; + this.notifyFailure(error); } try { writeContentPart((BMap) result, outputStream); } catch (Exception e) { - BError error = ErrorCreator.createError(StringUtils.fromString( - removeJavaExceptionPrefix(e.getMessage()))); - this.notifyFailure(error); + EntityBodyHandler.closeMessageOutputStream(outputStream); + throw ErrorCreator.createError(StringUtils.fromString( + "Error occurred while writing the stream content: " + + removeJavaExceptionPrefix(e.getMessage()))); } writeEvent(env, entity, outputStream, iteratorObj); } From d8b390da93f29e4e42a8f1ac7cef793a767bb02d Mon Sep 17 00:00:00 2001 From: MohamedSabthar Date: Wed, 24 Jul 2024 12:15:33 +0530 Subject: [PATCH 3/3] Handle Runtime Expection --- .../io/ballerina/stdlib/mime/nativeimpl/MimeEntityBody.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/src/main/java/io/ballerina/stdlib/mime/nativeimpl/MimeEntityBody.java b/native/src/main/java/io/ballerina/stdlib/mime/nativeimpl/MimeEntityBody.java index 0fbc7140..8a62f546 100644 --- a/native/src/main/java/io/ballerina/stdlib/mime/nativeimpl/MimeEntityBody.java +++ b/native/src/main/java/io/ballerina/stdlib/mime/nativeimpl/MimeEntityBody.java @@ -234,7 +234,7 @@ public static Object getStreamEntryRecord(BObject entityObj, long inputArraySize } while (arraySize > 0); bytes = output.toByteArray(); } - } catch (IOException ex) { + } catch (RuntimeException | IOException ex) { return IOUtils.createError(IOConstants.ErrorCode.GenericError, "Error occurred while reading stream:" + ex.getMessage()); }