From 6c991f89712a7701d30e8923617c63a9535d6c5c Mon Sep 17 00:00:00 2001 From: Nikita Kuprins Date: Thu, 24 Sep 2026 00:36:55 +0300 Subject: [PATCH 1/2] fix: add later failures as suppressed --- .../fesod/sheet/context/WriteContextImpl.java | 25 ++++++-- .../sheet/context/WriteContextImplTest.java | 63 +++++++++++++++++++ 2 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 fesod-sheet/src/test/java/org/apache/fesod/sheet/context/WriteContextImplTest.java diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java index fd118305d..19a25eb6a 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java @@ -541,7 +541,7 @@ public void finish(boolean onException) { try { isOutputStreamEncrypt = doOutputStreamEncrypt07(); } catch (Throwable t) { - throwable = t; + throwable = recordFailure(null, t); } } if (!isOutputStreamEncrypt) { @@ -551,7 +551,7 @@ public void finish(boolean onException) { } writeWorkbookHolder.getWorkbook().close(); } catch (Throwable t) { - throwable = t; + throwable = recordFailure(throwable, t); } } try { @@ -560,20 +560,20 @@ public void finish(boolean onException) { ((SXSSFWorkbook) workbook).dispose(); } } catch (Throwable t) { - throwable = t; + throwable = recordFailure(throwable, t); } try { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) { writeWorkbookHolder.getOutputStream().close(); } } catch (Throwable t) { - throwable = t; + throwable = recordFailure(throwable, t); } if (writeExcel && !isOutputStreamEncrypt) { try { doFileEncrypt07(); } catch (Throwable t) { - throwable = t; + throwable = recordFailure(throwable, t); } } try { @@ -581,7 +581,7 @@ public void finish(boolean onException) { writeWorkbookHolder.getTempTemplateInputStream().close(); } } catch (Throwable t) { - throwable = t; + throwable = recordFailure(throwable, t); } clearEncrypt03(); removeThreadLocalCache(); @@ -593,6 +593,19 @@ public void finish(boolean onException) { } } + /** + * Returns the first recorded failure, attaching any later one to it as a suppressed exception. + */ + private static Throwable recordFailure(Throwable recorded, Throwable t) { + if (recorded == null) { + return t; + } + if (t != null && recorded != t) { + recorded.addSuppressed(t); + } + return recorded; + } + /** * Removes thread-local caches used during the write process. */ diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/context/WriteContextImplTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/context/WriteContextImplTest.java new file mode 100644 index 000000000..b963d2e21 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/context/WriteContextImplTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.context; + +import java.io.IOException; +import java.io.OutputStream; +import org.apache.fesod.sheet.FesodSheet; +import org.apache.fesod.sheet.exception.ExcelGenerateException; +import org.apache.fesod.sheet.support.ExcelTypeEnum; +import org.apache.fesod.sheet.testkit.Tags; +import org.apache.fesod.sheet.testkit.builders.TestDataBuilder; +import org.apache.fesod.sheet.testkit.models.SimpleData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag(Tags.WRITE) +class WriteContextImplTest { + + /** + * When writing the workbook fails and closing the stream then fails too, the write failure is the cause. + */ + @Test + void finish_writeAndCloseFail_reportsWriteFailure() { + OutputStream brokenStream = new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new IOException("write failed"); + } + + @Override + public void close() throws IOException { + throw new IOException("close failed"); + } + }; + + ExcelGenerateException e = Assertions.assertThrows( + ExcelGenerateException.class, () -> FesodSheet.write(brokenStream, SimpleData.class) + .excelType(ExcelTypeEnum.XLSX) + .sheet() + .doWrite(TestDataBuilder.simpleData(10))); + Assertions.assertEquals("write failed", e.getCause().getMessage()); + Assertions.assertEquals(1, e.getCause().getSuppressed().length); + Assertions.assertEquals("close failed", e.getCause().getSuppressed()[0].getMessage()); + } +} From da104ff2f442843072332c25c0c23122f728a250 Mon Sep 17 00:00:00 2001 From: Nikita Kuprins Date: Thu, 8 Oct 2026 17:35:37 +0300 Subject: [PATCH 2/2] style: pass throwable in the first catch --- .../java/org/apache/fesod/sheet/context/WriteContextImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java index 19a25eb6a..17fcc7aad 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/context/WriteContextImpl.java @@ -541,7 +541,7 @@ public void finish(boolean onException) { try { isOutputStreamEncrypt = doOutputStreamEncrypt07(); } catch (Throwable t) { - throwable = recordFailure(null, t); + throwable = recordFailure(throwable, t); } } if (!isOutputStreamEncrypt) {