Skip to content

Fixed GH-3604, Add STErrorListener to StTemplateRenderer. #3606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.template.st;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.misc.ErrorType;
import org.stringtemplate.v4.misc.STMessage;

/**
* An ErrorListener for the {@link StTemplateRenderer} rendering exceptions.
* <p>
* By default, StringTemplate uses
* {@link org.stringtemplate.v4.misc.ErrorManager#DEFAULT_ERROR_LISTENER} as the exception
* handler, which outputs exceptions via System.err.println. This can lead to a loss of
* detailed exception logs from the user's perspective. The current ErrorListener retains
* the behavior of the default handler but additionally outputs the exceptions through
* logging mechanisms.
* </p>
*
* @author Sun Yuhan
*/
public class StTemplateRenderErrorListener implements STErrorListener {

private final Logger logger = LoggerFactory.getLogger(StTemplateRenderErrorListener.class);

@Override
public void compileTimeError(STMessage msg) {
logger.error(msg.toString());
}

@Override
public void runTimeError(STMessage msg) {
if (msg.error != ErrorType.NO_SUCH_PROPERTY) { // ignore these
logger.error(msg.toString());
}
}

@Override
public void IOError(STMessage msg) {
logger.error(msg.toString());
}

@Override
public void internalError(STMessage msg) {
logger.error(msg.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.compiler.Compiler;
import org.stringtemplate.v4.compiler.STLexer;

Expand All @@ -49,6 +50,7 @@
* is shared between threads.
*
* @author Thomas Vitale
* @author Sun Yuhan
* @since 1.0.0
*/
public class StTemplateRenderer implements TemplateRenderer {
Expand Down Expand Up @@ -112,7 +114,9 @@ public String apply(String template, Map<String, Object> variables) {

private ST createST(String template) {
try {
return new ST(template, this.startDelimiterToken, this.endDelimiterToken);
STGroup group = new STGroup(this.startDelimiterToken, this.endDelimiterToken);
group.setListener(new StTemplateRenderErrorListener());
return new ST(group, template);
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
Expand Down