samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
834 stars 128 forks source link

Template#Segment-> writing data in Writer - try-catch/autoclose? #112

Closed damir78 closed 9 months ago

damir78 commented 5 years ago

Hi guys, I think the part try { out.write(data); } can be rewritten in autocloseable style or pls complete try/catch with closing Writer (out.close() /..flush, etc.) `
protected static abstract class Segment { abstract void execute (Template tmpl, Context ctx, Writer out);

    abstract void decompile (Mustache.Delims delims, StringBuilder into);

    protected static void write (Writer out, String data) {
        try {
            out.write(data);
        } catch (IOException ioe) {
            throw new MustacheException(ioe);
        }
    }
}`
agentgt commented 2 years ago

You are incorrect. It is the responsibility of who creates the writer to close it. When handing off writers to other methods or classes they should never close the writer.

agentgt commented 9 months ago

@samskivert

I'm fairly sure we can close this issue.

One tangental thing that we could add in something like 2.0 (or possibly in a minor version depending on semver semantics) is to make a new exception class:

MustacheUncheckedIOException extends java.io.UncheckedIOException {
...
}

As IO exceptions really have nothing to do with MustacheExceptions but probably not worth doing (since it technically breaks API if people are expecting MustacheException for io issues.).