Skip to content

Commit c062aa7

Browse files
committed
Improving error handling on agent and plugin status report.
1 parent 317559d commit c062aa7

File tree

9 files changed

+302
-59
lines changed

9 files changed

+302
-59
lines changed

src/main/java/cd/go/contrib/elasticagent/executors/AgentStatusReportExecutor.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import cd.go.contrib.elasticagent.PluginRequest;
66
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
77
import cd.go.contrib.elasticagent.model.JobIdentifier;
8+
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationErrorHandler;
9+
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationException;
810
import cd.go.contrib.elasticagent.model.reports.agent.KubernetesElasticAgent;
911
import cd.go.contrib.elasticagent.requests.AgentStatusReportRequest;
1012
import com.google.gson.JsonObject;
@@ -36,7 +38,7 @@ public AgentStatusReportExecutor(AgentStatusReportRequest request, PluginRequest
3638
this.statusReportViewBuilder = builder;
3739
}
3840

39-
public GoPluginApiResponse execute() throws Exception {
41+
public GoPluginApiResponse execute() {
4042
String elasticAgentId = request.getElasticAgentId();
4143
JobIdentifier jobIdentifier = request.getJobIdentifier();
4244
LOG.info(format("[status-report] Generating status report for agent: {0} with job: {1}", elasticAgentId, jobIdentifier));
@@ -54,17 +56,12 @@ public GoPluginApiResponse execute() throws Exception {
5456

5557
final String statusReportView = statusReportViewBuilder.build(statusReportViewBuilder.getTemplate("agent-status-report.template.ftlh"), elasticAgent);
5658

57-
JsonObject responseJSON = new JsonObject();
59+
final JsonObject responseJSON = new JsonObject();
5860
responseJSON.addProperty("view", statusReportView);
5961

6062
return DefaultGoPluginApiResponse.success(responseJSON.toString());
6163
} catch (Exception e) {
62-
final String statusReportView = statusReportViewBuilder.build(statusReportViewBuilder.getTemplate("error.template.ftlh"), e);
63-
64-
JsonObject responseJSON = new JsonObject();
65-
responseJSON.addProperty("view", statusReportView);
66-
67-
return DefaultGoPluginApiResponse.success(responseJSON.toString());
64+
return StatusReportGenerationErrorHandler.handle(statusReportViewBuilder, e);
6865
}
6966
}
7067

@@ -74,7 +71,7 @@ private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesCli
7471
.withLabel(Constants.JOB_ID_LABEL_KEY, String.valueOf(jobIdentifier.getJobId()))
7572
.list().getItems().get(0);
7673
} catch (Exception e) {
77-
throw new RuntimeException(format("Can not find a running Pod for the provided job identifier ''{0}", jobIdentifier));
74+
throw new StatusReportGenerationException(format("Can not find a running Pod for the provided job identifier: {0}.", jobIdentifier.representation()));
7875
}
7976
}
8077

@@ -86,6 +83,6 @@ private Pod findPodUsingElasticAgentId(String elasticAgentId, KubernetesClient c
8683
}
8784
}
8885

89-
throw new RuntimeException(format("Can not find a running Pod for the provided elastic agent id ''{0}", elasticAgentId));
86+
throw new StatusReportGenerationException(format("Can not find a running Pod for the provided elastic agent id: {0}.", elasticAgentId));
9087
}
9188
}

src/main/java/cd/go/contrib/elasticagent/executors/StatusReportExecutor.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import cd.go.contrib.elasticagent.PluginRequest;
2121
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
2222
import cd.go.contrib.elasticagent.model.KubernetesCluster;
23+
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationErrorHandler;
2324
import com.google.gson.JsonObject;
2425
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2526
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2627
import freemarker.template.Template;
2728
import io.fabric8.kubernetes.client.KubernetesClient;
2829

29-
import java.io.IOException;
30-
3130
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3231

3332
public class StatusReportExecutor {
@@ -36,26 +35,30 @@ public class StatusReportExecutor {
3635
private final PluginStatusReportViewBuilder statusReportViewBuilder;
3736

3837

39-
public StatusReportExecutor(PluginRequest pluginRequest) throws IOException {
38+
public StatusReportExecutor(PluginRequest pluginRequest) {
4039
this(pluginRequest, KubernetesClientFactory.instance(), PluginStatusReportViewBuilder.instance());
4140
}
4241

43-
public StatusReportExecutor(PluginRequest pluginRequest, KubernetesClientFactory factory, PluginStatusReportViewBuilder statusReportViewBuilder) throws IOException {
42+
public StatusReportExecutor(PluginRequest pluginRequest, KubernetesClientFactory factory, PluginStatusReportViewBuilder statusReportViewBuilder) {
4443
this.pluginRequest = pluginRequest;
4544
this.factory = factory;
4645
this.statusReportViewBuilder = statusReportViewBuilder;
4746
}
4847

49-
public GoPluginApiResponse execute() throws Exception {
50-
LOG.info("[status-report] Generating status report");
51-
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
52-
final KubernetesCluster kubernetesCluster = new KubernetesCluster(client);
53-
final Template template = statusReportViewBuilder.getTemplate("status-report.template.ftlh");
54-
final String statusReportView = statusReportViewBuilder.build(template, kubernetesCluster);
48+
public GoPluginApiResponse execute() {
49+
try {
50+
LOG.info("[status-report] Generating status report");
51+
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
52+
final KubernetesCluster kubernetesCluster = new KubernetesCluster(client);
53+
final Template template = statusReportViewBuilder.getTemplate("status-report.template.ftlh");
54+
final String statusReportView = statusReportViewBuilder.build(template, kubernetesCluster);
5555

56-
JsonObject responseJSON = new JsonObject();
57-
responseJSON.addProperty("view", statusReportView);
56+
final JsonObject responseJSON = new JsonObject();
57+
responseJSON.addProperty("view", statusReportView);
5858

59-
return DefaultGoPluginApiResponse.success(responseJSON.toString());
59+
return DefaultGoPluginApiResponse.success(responseJSON.toString());
60+
} catch (Exception e) {
61+
return StatusReportGenerationErrorHandler.handle(statusReportViewBuilder, e);
62+
}
6063
}
6164
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent.model.reports;
18+
19+
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
20+
import cd.go.contrib.elasticagent.model.reports.agent.StatusReportGenerationError;
21+
import com.google.gson.JsonObject;
22+
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
23+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
24+
import freemarker.template.Template;
25+
26+
import static java.text.MessageFormat.format;
27+
28+
public class StatusReportGenerationErrorHandler {
29+
30+
public static GoPluginApiResponse handle(PluginStatusReportViewBuilder builder, Exception e) {
31+
try {
32+
final Template template = builder.getTemplate("error.template.ftlh");
33+
final String errorView = builder.build(template, new StatusReportGenerationError(e));
34+
35+
final JsonObject responseJSON = new JsonObject();
36+
responseJSON.addProperty("view", errorView);
37+
38+
return DefaultGoPluginApiResponse.success(responseJSON.toString());
39+
} catch (Exception ex) {
40+
return DefaultGoPluginApiResponse.error(format("Error while generating status report: {0}.", e.toString()));
41+
}
42+
}
43+
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2018 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent.model.reports;
18+
19+
public class StatusReportGenerationException extends RuntimeException {
20+
private final String message;
21+
private final String detailedMessage;
22+
23+
public StatusReportGenerationException(String message) {
24+
this(message, null);
25+
}
26+
27+
public StatusReportGenerationException(String message, String detailedMessage) {
28+
this.message = message;
29+
this.detailedMessage = detailedMessage;
30+
}
31+
32+
public String getMessage() {
33+
return message;
34+
}
35+
36+
public String getDetailedMessage() {
37+
return detailedMessage;
38+
}
39+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2018 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent.model.reports.agent;
18+
19+
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationException;
20+
import org.apache.commons.lang3.StringUtils;
21+
22+
import java.io.Closeable;
23+
import java.io.PrintWriter;
24+
import java.io.StringWriter;
25+
26+
public class StatusReportGenerationError {
27+
private String stacktrace;
28+
private String message;
29+
30+
public StatusReportGenerationError(Throwable throwable) {
31+
if (throwable instanceof StatusReportGenerationException) {
32+
this.message = throwable.getMessage();
33+
this.stacktrace = ((StatusReportGenerationException) throwable).getDetailedMessage();
34+
} else {
35+
this.stacktrace = initStacktrace(throwable);
36+
this.message = initMessage(throwable);
37+
}
38+
}
39+
40+
public String getStacktrace() {
41+
return stacktrace;
42+
}
43+
44+
public String getMessage() {
45+
return message;
46+
}
47+
48+
private String initMessage(Throwable throwable) {
49+
if (StringUtils.isNotBlank(throwable.getMessage())) {
50+
return throwable.getMessage();
51+
}
52+
53+
if (StringUtils.isNotBlank(this.stacktrace)) {
54+
return stacktrace.split("\n")[0];
55+
}
56+
57+
return null;
58+
}
59+
60+
private String initStacktrace(Throwable throwable) {
61+
StringWriter sw = new StringWriter();
62+
PrintWriter pw = new PrintWriter(sw);
63+
try {
64+
throwable.printStackTrace(pw);
65+
String stacktrace = sw.toString();
66+
return stacktrace;
67+
} finally {
68+
closeQuietly(sw);
69+
closeQuietly(pw);
70+
}
71+
}
72+
73+
private void closeQuietly(Closeable closeable) {
74+
try {
75+
closeable.close();
76+
} catch (Exception e) {
77+
//Ignore
78+
}
79+
}
80+
}
Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,80 @@
11
<style>
2-
[data-plugin-style-id="kubernetes-plugin"] .outer-container {
3-
display: table;
4-
position: absolute;
5-
height: calc(100vh - 119px);
6-
width: 100%;
7-
}
8-
9-
[data-plugin-style-id="kubernetes-plugin"] .container {
10-
display: table-cell;
11-
vertical-align: middle;
12-
}
13-
14-
[data-plugin-style-id="kubernetes-plugin"] .message {
15-
margin-left: auto;
16-
margin-right: auto;
17-
18-
background: #FFF;
19-
padding: 20px;
20-
color: #991A00;
21-
font-size: large;
22-
text-align: center;
23-
vertical-align: middle;
24-
}
2+
[data-plugin-style-id="kubernetes-plugin"] .outer-container {
3+
display: table;
4+
position: absolute;
5+
height: calc(100vh - 119px);
6+
width: 100%;
7+
}
8+
9+
[data-plugin-style-id="kubernetes-plugin"] .container {
10+
display: table-cell;
11+
}
12+
13+
[data-plugin-style-id="kubernetes-plugin"] .error-container {
14+
width: 100%;
15+
border: 1px solid #eee;
16+
display: table;
17+
margin: 15px 0px;
18+
table-layout: fixed;
19+
border-radius: 3px;
20+
}
21+
22+
[data-plugin-style-id="kubernetes-plugin"] .title {
23+
padding: 1rem;
24+
overflow: hidden;
25+
font-size: 15px;
26+
background: #d1c4e9;
27+
font-weight: 600;
28+
vertical-align: middle;
29+
}
30+
31+
[data-plugin-style-id="kubernetes-plugin"] .stacktrace {
32+
height: calc(100vh - 210px);
33+
}
34+
35+
[data-plugin-style-id="kubernetes-plugin"] .no-stacktrace {
36+
color: #777;
37+
padding: 10px;
38+
font-size: 15px;
39+
font-weight: 400;
40+
background: #FAFAFA;
41+
}
42+
43+
[data-plugin-style-id="kubernetes-plugin"] textarea[readonly="readonly"], textarea[readonly] {
44+
cursor: auto;
45+
margin-bottom: 0px;
46+
}
47+
48+
[data-plugin-style-id="kubernetes-plugin"] .logs {
49+
color: white;
50+
display: block;
51+
height: calc(100vh - 210px);
52+
font-size: 13px;
53+
max-height: 100%;
54+
font-weight: 400;
55+
font-family: monaco;
56+
padding-left: 10px;
57+
background-color: #383838;
58+
}
2559
</style>
2660

2761
<div data-plugin-style-id="kubernetes-plugin">
28-
<div class="outer-container">
29-
<div class="container">
30-
<div class="message">
31-
${ message! }
32-
</div>
33-
</div>
34-
</div>
62+
<div class="outer-container">
63+
<div class="container">
64+
<div class="error-container">
65+
<div class="title">
66+
Error while generating status report: ${ message! }
67+
</div>
68+
<#if stacktrace??>
69+
<div class="stacktrace">
70+
<textarea class="logs" readonly>${stacktrace!}</textarea>
71+
</div>
72+
<#else>
73+
<div class="no-stacktrace">
74+
Stacktrace not available.
75+
</div>
76+
</#if>
77+
</div>
78+
</div>
79+
</div>
3580
</div>

0 commit comments

Comments
 (0)