From 71ba37f27f9a97b7116da997e8ed7403e4f9dadf Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Mon, 14 Oct 2024 16:18:23 +0200 Subject: [PATCH] LDEV-5010 - improve exception message --- .../runtime/functions/file/FileWrite.java | 22 ++++++++++++++++--- loader/build.xml | 2 +- loader/pom.xml | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/lucee/runtime/functions/file/FileWrite.java b/core/src/main/java/lucee/runtime/functions/file/FileWrite.java index 74b91954c6..5c77a0a4cf 100755 --- a/core/src/main/java/lucee/runtime/functions/file/FileWrite.java +++ b/core/src/main/java/lucee/runtime/functions/file/FileWrite.java @@ -19,6 +19,7 @@ package lucee.runtime.functions.file; import java.io.IOException; +import java.io.UnsupportedEncodingException; import lucee.commons.io.IOUtil; import lucee.commons.io.res.Resource; @@ -28,6 +29,7 @@ import lucee.runtime.PageContextImpl; import lucee.runtime.PageSourcePool; import lucee.runtime.exp.ApplicationException; +import lucee.runtime.exp.ExpressionException; import lucee.runtime.exp.PageException; import lucee.runtime.op.Caster; @@ -63,9 +65,7 @@ public static String call(PageContext pc, Object obj, Object data, String charse fsw.write(data); } catch (IOException e) { - ApplicationException ae = new ApplicationException("Invalid file [" + Caster.toResource(pc, obj, false) + "]"); - ExceptionUtil.initCauseEL(ae, e); - throw ae; + throw toApplicationException(pc, obj, charset, e); } finally { if (close) IOUtil.closeEL(fsw); @@ -74,4 +74,20 @@ public static String call(PageContext pc, Object obj, Object data, String charse return null; } + + private static ApplicationException toApplicationException(PageContext pc, Object obj, String charset, IOException e) throws ExpressionException { + ApplicationException ae; + if (e instanceof UnsupportedEncodingException) { + ae = new ApplicationException("Failed to write to file [" + Caster.toResource(pc, obj, false) + "], because the given charset [" + charset + "] is not supported"); + + } + else { + String msg = e.getMessage(); + String appendix = StringUtil.isEmpty(msg, true) ? "" : (", caused by [" + msg + "]"); + ae = new ApplicationException("Failed to write to file [" + Caster.toResource(pc, obj, false) + "]" + appendix); + + } + ExceptionUtil.initCauseEL(ae, e); + return ae; + } } \ No newline at end of file diff --git a/loader/build.xml b/loader/build.xml index a20ad36ff5..158f6fefa9 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 92696fa7b1..e666988d0a 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.1.1.98-SNAPSHOT + 6.1.1.99-SNAPSHOT jar Lucee Loader Build