| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2023-2025 the original author or authors.  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +package org.springframework.ai.template.st;  | 
 | 18 | + | 
 | 19 | +import org.slf4j.Logger;  | 
 | 20 | +import org.slf4j.LoggerFactory;  | 
 | 21 | +import org.stringtemplate.v4.STErrorListener;  | 
 | 22 | +import org.stringtemplate.v4.misc.ErrorType;  | 
 | 23 | +import org.stringtemplate.v4.misc.STMessage;  | 
 | 24 | + | 
 | 25 | +/**  | 
 | 26 | + * An ErrorListener for the {@link StTemplateRenderer} rendering exceptions.  | 
 | 27 | + * <p>  | 
 | 28 | + * By default, StringTemplate uses  | 
 | 29 | + * {@link org.stringtemplate.v4.misc.ErrorManager#DEFAULT_ERROR_LISTENER} as the exception  | 
 | 30 | + * handler, which outputs exceptions via System.err.println. This can lead to a loss of  | 
 | 31 | + * detailed exception logs from the user's perspective. The current ErrorListener retains  | 
 | 32 | + * the behavior of the default handler but additionally outputs the exceptions through  | 
 | 33 | + * logging mechanisms.  | 
 | 34 | + * </p>  | 
 | 35 | + *  | 
 | 36 | + * @author Sun Yuhan  | 
 | 37 | + */  | 
 | 38 | +public class StTemplateRenderErrorListener implements STErrorListener {  | 
 | 39 | + | 
 | 40 | +	private final Logger logger = LoggerFactory.getLogger(StTemplateRenderErrorListener.class);  | 
 | 41 | + | 
 | 42 | +	@Override  | 
 | 43 | +	public void compileTimeError(STMessage msg) {  | 
 | 44 | +		logger.error(msg.toString());  | 
 | 45 | +	}  | 
 | 46 | + | 
 | 47 | +	@Override  | 
 | 48 | +	public void runTimeError(STMessage msg) {  | 
 | 49 | +		if (msg.error != ErrorType.NO_SUCH_PROPERTY) { // ignore these  | 
 | 50 | +			logger.error(msg.toString());  | 
 | 51 | +		}  | 
 | 52 | +	}  | 
 | 53 | + | 
 | 54 | +	@Override  | 
 | 55 | +	public void IOError(STMessage msg) {  | 
 | 56 | +		logger.error(msg.toString());  | 
 | 57 | +	}  | 
 | 58 | + | 
 | 59 | +	@Override  | 
 | 60 | +	public void internalError(STMessage msg) {  | 
 | 61 | +		logger.error(msg.toString());  | 
 | 62 | +	}  | 
 | 63 | + | 
 | 64 | +}  | 
0 commit comments