RestTemplate:从 ResponseErrorHandler 抛出的自定义异常未传播到调用方
RestTemplate: Custom Exception thrown from ResponseErrorHandler is NOT Propagating to Caller
我正在开发 API。按照 Clean Code 和其他干净的编程指南,我 不想用 try/catch 块 弄乱我的代码。但是我面临一个挑战:从 ResponseErrorHandler 抛出的自定义异常没有传播给调用者;相反,调用者收到的是 ResourceAccessException。
这是我的 API 代码(为简洁起见,省略了无关紧要的部分)。请注意,我没有在 try 块中围绕 restTemplate.delete() 调用 - 这是故意的:
restTemplate.setErrorHandler(ecmResponseErrorHandler);
restTemplate.delete(serviceUrl, params);
我的 ecmResponseErrorHandler 代码(为简洁起见省略了不敬的部分):
public class ECMResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode() != HttpStatus.OK) {
//logging etc.
return true;
}
return false;
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
//appropriately populate ECMException() and then throw it
throw new ECMException();
}
}
我的 ECMException 是(为简洁起见省略了无关紧要的部分):
public class ECMException extends IOException {
public ECMException() {
super();
}
//etc.
}
现在,我的 JUnit 测试用例没有收到 ECMException,而是收到了 ResourceAccessException:
java.lang.Exception: Unexpected exception, expected<com.db.dbbpm.ecmfacade.exception.ECMException> but was<org.springframework.web.client.ResourceAccessException>
如果我将 API 代码更改为以下,一切正常;但是我不想用 try/catch 块使我的 API 代码混乱:
restTemplate.setErrorHandler(ecmResponseErrorHandler);
try {
restTemplate.delete(serviceUrl, params);
} catch () {
throw new ECMException();
}
为什么从 ResponseErrorHandler 抛出的 ECMException 没有向上传播到调用者?我该如何实现?
使 ECMException 扩展 RuntimeException(而不是 IOException)解决了问题。现在我的代码更简洁了。
public class ECMException extends RuntimeException {
public ECMException() {
super();
}
//etc.
}
我正在开发 API。按照 Clean Code 和其他干净的编程指南,我 不想用 try/catch 块 弄乱我的代码。但是我面临一个挑战:从 ResponseErrorHandler 抛出的自定义异常没有传播给调用者;相反,调用者收到的是 ResourceAccessException。
这是我的 API 代码(为简洁起见,省略了无关紧要的部分)。请注意,我没有在 try 块中围绕 restTemplate.delete() 调用 - 这是故意的:
restTemplate.setErrorHandler(ecmResponseErrorHandler);
restTemplate.delete(serviceUrl, params);
我的 ecmResponseErrorHandler 代码(为简洁起见省略了不敬的部分):
public class ECMResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode() != HttpStatus.OK) {
//logging etc.
return true;
}
return false;
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
//appropriately populate ECMException() and then throw it
throw new ECMException();
}
}
我的 ECMException 是(为简洁起见省略了无关紧要的部分):
public class ECMException extends IOException {
public ECMException() {
super();
}
//etc.
}
现在,我的 JUnit 测试用例没有收到 ECMException,而是收到了 ResourceAccessException:
java.lang.Exception: Unexpected exception, expected<com.db.dbbpm.ecmfacade.exception.ECMException> but was<org.springframework.web.client.ResourceAccessException>
如果我将 API 代码更改为以下,一切正常;但是我不想用 try/catch 块使我的 API 代码混乱:
restTemplate.setErrorHandler(ecmResponseErrorHandler);
try {
restTemplate.delete(serviceUrl, params);
} catch () {
throw new ECMException();
}
为什么从 ResponseErrorHandler 抛出的 ECMException 没有向上传播到调用者?我该如何实现?
使 ECMException 扩展 RuntimeException(而不是 IOException)解决了问题。现在我的代码更简洁了。
public class ECMException extends RuntimeException {
public ECMException() {
super();
}
//etc.
}