Page 1 of 1

Log and throw anti-pattern detected

PostPosted: Wed Dec 21, 2016 1:25 am
by uffe_w
We have a situation where client code sometimes cancels the request to generate a preview and that means that the parser in docx4j will throw an exception. The exception ends up in the XMLUtils inner class LoggingErrorListener, which in case of a fatal error both logs and throws the exception.
We would prefer that the exceptions only were thrown and not logged so we can handle them ourselves in our code and not have docx4j log a complete stacktrace every time this happens.

Can this be changed?

Re: Log and throw anti-pattern detected

PostPosted: Wed Dec 21, 2016 10:57 am
by jason
Makes sense, how about https://github.com/plutext/docx4j/commi ... db4acfef6d

Unfortunately, this just missed v3.3.2

Re: Log and throw anti-pattern detected

PostPosted: Wed Dec 21, 2016 6:30 pm
by uffe_w
Thanks, that will do nicely :)

Looking forward to v3.3.3 then...

Re: Log and throw anti-pattern detected

PostPosted: Wed Dec 21, 2016 11:36 pm
by uffe_w
I think the problem is in AbstractExporter.export() as well, can the problem be fixed there too?

} catch (Exception e) {
log.error("Exception exporting package", e);
throw new Docx4JException("Exception exporting package", e);
}

This is actually exactly why the log and throw antipattern should be avoided. Now the same stacktrace is logged more than once.