All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.summerb.utils.exceptions.translator.ExceptionTranslatorClassNameImpl Maven / Gradle / Ivy

There is a newer version: 4.1.1
Show newest version
package org.summerb.utils.exceptions.translator;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;

/**
 * This translator treats class name as a message code and it's message as a
 * first argument message
 * 
 * @author sergeyk
 *
 */
public class ExceptionTranslatorClassNameImpl implements ExceptionTranslator {
	private MessageSource messageSource;

	@Autowired
	public ExceptionTranslatorClassNameImpl(MessageSource messageSource2) {
		messageSource = messageSource2;
	}

	@Override
	public String buildUserMessage(Throwable t, Locale locale) {
		try {
			String className = t.getClass().getName();
			String messageMappingForClassName = messageSource.getMessage(className, new Object[] { t.getMessage() },
					locale);
			return messageMappingForClassName;
		} catch (NoSuchMessageException nfe) {
			return t.getClass().getSimpleName() + " (" + t.getLocalizedMessage() + ")";
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy