|
18 | 18 | package net.croz.nrich.notification.service;
|
19 | 19 |
|
20 | 20 | import lombok.RequiredArgsConstructor;
|
| 21 | +import net.croz.nrich.core.api.exception.ExceptionWithArguments; |
| 22 | +import net.croz.nrich.core.api.exception.ExceptionWithMessage; |
| 23 | +import net.croz.nrich.core.api.exception.ExceptionWithMessageCode; |
21 | 24 | import net.croz.nrich.notification.api.model.AdditionalNotificationData;
|
22 | 25 | import net.croz.nrich.notification.api.model.Notification;
|
23 | 26 | import net.croz.nrich.notification.api.model.NotificationSeverity;
|
@@ -87,15 +90,15 @@ public ValidationFailureNotification createNotificationForValidationFailure(Cons
|
87 | 90 | }
|
88 | 91 |
|
89 | 92 | @Override
|
90 |
| - public Notification createNotificationForException(Throwable throwable, AdditionalNotificationData additionalNotificationData, Object... exceptionMessageArgumentList) { |
| 93 | + public Notification createNotificationForException(Throwable throwable, AdditionalNotificationData additionalNotificationData) { |
91 | 94 | String typeName = throwable.getClass().getName();
|
92 | 95 | String titleCode = String.format(NotificationConstants.PREFIX_MESSAGE_FORMAT, typeName, NotificationConstants.MESSAGE_TITLE_SUFFIX);
|
93 | 96 |
|
94 | 97 | String title = notificationMessageResolverService.resolveMessage(toList(titleCode, NotificationConstants.ERROR_OCCURRED_MESSAGE_TITLE_CODE), NotificationConstants.EMPTY_MESSAGE);
|
95 |
| - String contentCode = String.format(NotificationConstants.PREFIX_MESSAGE_FORMAT, typeName, NotificationConstants.MESSAGE_CONTENT_SUFFIX); |
| 98 | + |
96 | 99 | String severityCode = String.format(NotificationConstants.PREFIX_MESSAGE_FORMAT, typeName, NotificationConstants.MESSAGE_SEVERITY_SUFFIX);
|
| 100 | + String content = resolveExceptionContent(throwable); |
97 | 101 |
|
98 |
| - String content = notificationMessageResolverService.resolveMessage(toList(contentCode, NotificationConstants.ERROR_OCCURRED_DEFAULT_CODE), toList(exceptionMessageArgumentList), null); |
99 | 102 | NotificationSeverity severity = Optional.ofNullable(additionalNotificationData.getSeverity()).orElse(resolveExceptionSeverity(severityCode));
|
100 | 103 | List<String> messageList = resolveMessageListFromNotificationData(additionalNotificationData.getMessageListDataMap());
|
101 | 104 |
|
@@ -159,8 +162,30 @@ private NotificationSeverity resolveExceptionSeverity(String messageCode) {
|
159 | 162 | return NotificationSeverity.valueOf(severityValue);
|
160 | 163 | }
|
161 | 164 |
|
| 165 | + private String resolveExceptionContent(Throwable throwable) { |
| 166 | + if (throwable instanceof ExceptionWithMessage exceptionWithMessage) { |
| 167 | + return exceptionWithMessage.getMessage(); |
| 168 | + } |
| 169 | + |
| 170 | + String contentCode; |
| 171 | + if (throwable instanceof ExceptionWithMessageCode exceptionWithMessageCode) { |
| 172 | + contentCode = exceptionWithMessageCode.getMessageCode(); |
| 173 | + } |
| 174 | + else { |
| 175 | + String typeName = throwable.getClass().getName(); |
| 176 | + contentCode = String.format(NotificationConstants.PREFIX_MESSAGE_FORMAT, typeName, NotificationConstants.MESSAGE_CONTENT_SUFFIX); |
| 177 | + } |
| 178 | + |
| 179 | + List<Object> argumentList = new ArrayList<>(); |
| 180 | + if (throwable instanceof ExceptionWithArguments exceptionWithArguments) { |
| 181 | + argumentList.addAll(toList(exceptionWithArguments.getArgumentList())); |
| 182 | + } |
| 183 | + |
| 184 | + return notificationMessageResolverService.resolveMessage(toList(contentCode, NotificationConstants.ERROR_OCCURRED_DEFAULT_CODE), argumentList, null); |
| 185 | + } |
| 186 | + |
162 | 187 | @SafeVarargs
|
163 |
| - private final <T> List<T> toList(T... codeList) { |
| 188 | + private <T> List<T> toList(T... codeList) { |
164 | 189 | if (codeList == null) {
|
165 | 190 | return Collections.emptyList();
|
166 | 191 | }
|
|
0 commit comments