Skip to content

[Enhancement] Report the first failure from write finish() instead of the last #1136

Description

@nkuprins

Search before asking

  • 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:

Throwable throwable = null;
...
try {
    workbook.write(outputStream);    // 1. fails
    workbook.close();
} catch (Throwable t) {
    throwable = t;                   //    recorded
}
...
try {
    outputStream.close();            // 2. fails too, because the stream is already broken
} catch (Throwable t) {
    throwable = t;                   //    overwrites 1.
}
...
if (throwable != null) {
    throw new ExcelGenerateException("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:

} catch (Throwable t) {
    if (throwable == null) {
        throwable = t;
    }
}

Alternatives

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.

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions