Skip to content

Commit

Permalink
Exception can appear in different places in the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoop committed Oct 4, 2024
1 parent db3c1fe commit 67ad56a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/changelog/114002.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pr: 114002
summary: Add a size limit to outputs from mustache
summary: Add a `mustache.result.size.limit` option to limit the length of results from mustache scripts
area: Infra/Scripting
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.SizeLimitingStringWriter;
Expand Down Expand Up @@ -141,21 +142,22 @@ public String execute() {
try {
template.execute(writer, params);
} catch (Exception e) {
// size limit exception can appear at several places in the causal list depending on script & context
if (ExceptionsHelper.unwrap(e, SizeLimitingStringWriter.SizeLimitExceededException.class) != null) {
// don't log, client problem
throw new ElasticsearchParseException("Mustache script result size limit exceeded", e);
}
if (shouldLogException(e)) {
logger.error(() -> format("Error running %s", template), e);
}
if (e.getCause() instanceof SizeLimitingStringWriter.SizeLimitExceededException) {
throw new ElasticsearchParseException("Mustache script result size limit exceeded", e);
}
throw new GeneralScriptException("Error running " + template, e);
}
return writer.toString();
}

public boolean shouldLogException(Throwable e) {
return e.getCause() != null
&& e.getCause() instanceof MustacheInvalidParameterException == false
&& e.getCause() instanceof SizeLimitingStringWriter.SizeLimitExceededException == false;
&& e.getCause() instanceof MustacheInvalidParameterException == false;
}
}

Expand Down

0 comments on commit 67ad56a

Please sign in to comment.