包装此异常的好方法是什么?

问题描述 投票:1回答:1

我正在为我的springboot应用程序实现HealthIndicator,该应用程序ping Jenkins实例以检查它们是否启动。我通过对${JENKINS_URL}/login执行ping操作。如果到了,我会收到200条回复。如果没有启动,我会得到一个非常丑陋的堆栈跟踪,如下所示:

org.springframework.http.InvalidMediaTypeException: Invalid mime type "html": does not contain '/'
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:574) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:966) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.getCharset(DefaultResponseErrorHandler.java:224) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:165) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:785) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:345) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at com.path.to.package.JenkinsCache.pingJenkins(JenkinsCache.java:60) [classes/:na]

我的驱动程序代码如下所示(在JenkinsCache中:]

private String pingJenkins(String jenkinsEndpoint) {
     RestTemplate restTemplate = new RestTemplate();
     try{
            restTemplate.getForEntity(jenkinsEndpoint+"/login", String.class);
            return "OK";
        } catch (Exception e) {
            log.error("An error occurred while pinging: "+jenkinsEndpoint, e);
        }
        return "DOWN";
    }

我宁愿捕获特定的异常,但是如果对关闭的Jenkins实例执行ping操作会产生InvalidMediaTypeException,那么它看起来会有点混乱的代码(即,我希望像RestClientException之类的东西,等等)。捕获/处理此异常的好方法是什么?我应该代替RuntimeException吗?

java spring-boot exception spring-boot-actuator
1个回答
0
投票

好吧,如果you知道此调用返回的预期异常是InvalidMediaTypeException,则只需创建一个RestClientException并将其重新扔给调用pingJenkins的调用方。然后,使用您的API的任何人都会期望RestClientException。由于这是Spring Boot,因此您甚至可以使用全局异常处理程序enter link description here来捕获该异常,而无需这样做。

public void callingMethod() {
    try {
        pingJenkins("http://www.jenkins-endpoint.com");
    } catch (RestClientException exception) {
        log.error("Could not connect to Jenkins", exception);
    } 
}

private String pingJenkins(String jenkinsEndpoint) {
    RestTemplate restTemplate = new RestTemplate();
    try{
        restTemplate.getForEntity(jenkinsEndpoint+"/login", String.class);
        return "OK";
    } catch (InvalidMediaTypeException exception) {
        throw new RestClientException("Pinging Jenkins failed: " + exception.getMessage());
    }

    return "DOWN";
}
© www.soinside.com 2019 - 2024. All rights reserved.