Skip to content

Commit f32fb96

Browse files
committed
<fix>[errorcode]: address review — null-safe message fallback and avoid loader init
- GlobalErrorCodeI18nServiceImpl: add final fallback to error.code/empty string when both details and description are null, guaranteeing message is never null - Platform: use existing loader field instead of getComponentLoader() to avoid triggering ComponentLoader creation during early startup; move message fallback outside catch block so it always runs Co-Authored-By: ye.zou <ye.zou@zstack.io> Change-Id: I8571f05657dc2173cc232b511f505eedc68d714e
1 parent 1dc68d2 commit f32fb96

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

core/src/main/java/org/zstack/core/Platform.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -992,15 +992,18 @@ public static ErrorCode err(String globalErrorCode, Enum errCode, ErrorCode caus
992992
// populate message at creation time with default locale;
993993
// RestServer will override with client's Accept-Language if different
994994
try {
995-
GlobalErrorCodeI18nService i18nService = getComponentLoader().getComponent(GlobalErrorCodeI18nService.class);
996-
if (i18nService != null) {
997-
i18nService.localizeErrorCode(result, org.zstack.core.errorcode.LocaleUtils.DEFAULT_LOCALE);
995+
ComponentLoader currentLoader = loader;
996+
if (currentLoader != null) {
997+
GlobalErrorCodeI18nService i18nService = currentLoader.getComponent(GlobalErrorCodeI18nService.class);
998+
if (i18nService != null) {
999+
i18nService.localizeErrorCode(result, org.zstack.core.errorcode.LocaleUtils.DEFAULT_LOCALE);
1000+
}
9981001
}
9991002
} catch (Exception e) {
1000-
// i18n service not initialized during early startup, use details as fallback
1001-
if (result.getMessage() == null) {
1002-
result.setMessage(details);
1003-
}
1003+
// i18n service not initialized during early startup
1004+
}
1005+
if (result.getMessage() == null) {
1006+
result.setMessage(details != null ? details : result.getDescription());
10041007
}
10051008

10061009
return result;

core/src/main/java/org/zstack/core/errorcode/GlobalErrorCodeI18nServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public void localizeErrorCode(ErrorCode error, String locale) {
140140

141141
// guarantee: message is never null
142142
if (error.getMessage() == null) {
143-
error.setMessage(error.getDetails() != null ? error.getDetails() : error.getDescription());
143+
String fallback = error.getDetails() != null ? error.getDetails() : error.getDescription();
144+
error.setMessage(fallback != null ? fallback : (error.getCode() != null ? error.getCode() : ""));
144145
}
145146

146147
if (error.getCause() != null) {

0 commit comments

Comments
 (0)