You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Enhancement] Report the first failure from write finish() instead of the last #1136
I searched in the issues and found nothing similar.
Motivation
WriteContextImpl#finish(boolean) runs its steps one after another and collects failures into a single throwable. Each catch overwrites it, so when more than one step fails, the caller only sees the last failure (code). Shortened:
Throwablethrowable = null;
...
try {
workbook.write(outputStream); // 1. failsworkbook.close();
} catch (Throwablet) {
throwable = t; // recorded
}
...
try {
outputStream.close(); // 2. fails too, because the stream is already broken
} catch (Throwablet) {
throwable = t; // overwrites 1.
}
...
if (throwable != null) {
thrownewExcelGenerateException("Can not close IO.", throwable); // cause = 2.
}
Solution
The first catch can't overwrite anything. In the five after it, keep the exception only if none has been recorded yet:
Attach later failures with addSuppressed. Nothing is lost, but each catch gets a helper call, and later failures are usually consequences of the first anyway.
Anything else?
For background: when this code was first written, each of these catch blocks threw straight away, so the first failure was the one reported. alibaba/easyexcel@2c8918be changed them to save the exception and carry on, so the later close steps would still run.
Activity