Skip to content

Commit 4bc2d92

Browse files
committed
Exception is thrown when component failed to register
Signed-off-by: JermaineHua <[email protected]>
1 parent c53e51c commit 4bc2d92

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private ComponentInfo doRegister(ComponentInfo ci) {
187187
ci.register();
188188
} catch (Throwable t) {
189189
LOGGER.error(ErrorCode.convert("01-03003", ci.getName()), t);
190-
return null;
190+
throw new ServiceRuntimeException(ErrorCode.convert("01-03003", ci.getName()));
191191
}
192192

193193
LOGGER.info("Registering component: {}", ci.getName());
@@ -209,6 +209,7 @@ private ComponentInfo doRegister(ComponentInfo ci) {
209209
} catch (Throwable t) {
210210
ci.exception(new Exception(t));
211211
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t);
212+
throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName()));
212213
}
213214

214215
return ci;

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/impl/ComponentManagerImplTests.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,27 @@ public void registerException(CapturedOutput capturedOutput) {
138138
componentInfoA = new DemoComponent("A");
139139
componentInfoA.setRegisterException(true);
140140

141-
assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
142-
assertThat(capturedOutput.getOut()).contains("01-03003");
143-
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
141+
try {
142+
assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
143+
} catch (ServiceRuntimeException e) {
144+
assertThat(capturedOutput.getOut()).contains("01-03003");
145+
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
146+
}
144147
}
145148

146149
@Test
147150
public void resolveException(CapturedOutput capturedOutput) {
148151
componentInfoA = new DemoComponent("A");
149152
componentInfoA.setResolveException(true);
150153

151-
assertThat(componentManager.registerAndGet(componentInfoA)).isEqualTo(componentInfoA);
152-
assertThat(componentInfoA.isHealthy().isHealthy()).isFalse();
153-
assertThat(capturedOutput.getOut()).contains("01-03004");
154-
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
154+
try {
155+
assertThat(componentManager.registerAndGet(componentInfoA)).isEqualTo(componentInfoA);
156+
157+
} catch (ServiceRuntimeException e) {
158+
assertThat(componentInfoA.isHealthy().isHealthy()).isFalse();
159+
assertThat(capturedOutput.getOut()).contains("01-03004");
160+
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
161+
}
155162
}
156163

157164
@Test

0 commit comments

Comments
 (0)