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
12
13
14
15 public abstract class BaseApplicationException extends RuntimeException {
16
17 private static final long serialVersionUID = 1L;
18
19
20
21
22
23
24
25 protected BaseApplicationException(final String msg, final Object... params) {
26 super(getFinalMessage(msg, params));
27 }
28
29
30
31
32
33
34
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 }