View Javadoc
1   package org.imageconverter.infra.exception;
2   
3   import static org.imageconverter.util.BeanUtil.getBeanFrom;
4   
5   import org.apache.commons.lang3.RegExUtils;
6   import org.apache.commons.lang3.StringUtils;
7   import org.springframework.context.MessageSource;
8   import org.springframework.context.i18n.LocaleContextHolder;
9   
10  /**
11   * Base applicaton exception.
12   * 
13   * @author Fernando Romulo da Silva
14   */
15  public abstract class BaseApplicationException extends RuntimeException {
16  
17      private static final long serialVersionUID = 1L;
18  
19      /**
20       * Constructs a new BaseApplicationException exception with the specified detail message.
21       * 
22       * @param msg    The detail message
23       * @param params The parameters used on message
24       */
25      protected BaseApplicationException(final String msg, final Object... params) {
26  	super(getFinalMessage(msg, params));
27      }
28  
29      /**
30       * Constructs a new runtime exception with the specified detail message and cause.
31       * 
32       * @param msg    The detail message
33       * @param ex     The cause
34       * @param params The parameters used on message
35       */
36      protected BaseApplicationException(final String msg, final Throwable ex, final Object... params) {
37  	super(getFinalMessage(msg, params), ex);
38      }
39  
40      private static String getFinalMessage(final String msg, final Object... params) {
41  
42  	if (StringUtils.containsNone(msg, '{', '}')) {
43  	    return msg;
44  	}
45  	
46  	final var code = RegExUtils.replaceAll(msg, "[{}]", "");
47  	final var messageSource = getBeanFrom(MessageSource.class);
48  	final var locale = LocaleContextHolder.getLocale();
49  
50  	return messageSource.getMessage(code, params, locale);
51      }
52  }