1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.exception;
20
21 import java.io.IOException;
22 import java.net.ConnectException;
23 import java.net.UnknownHostException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.inject.Named;
27 import javax.inject.Singleton;
28 import org.apache.maven.lifecycle.LifecycleExecutionException;
29 import org.apache.maven.model.building.ModelProblem;
30 import org.apache.maven.model.building.ModelProblemUtils;
31 import org.apache.maven.plugin.AbstractMojoExecutionException;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.PluginContainerException;
35 import org.apache.maven.plugin.PluginExecutionException;
36 import org.apache.maven.project.ProjectBuildingException;
37 import org.apache.maven.project.ProjectBuildingResult;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Named
89 @Singleton
90 public class DefaultExceptionHandler implements ExceptionHandler {
91
92 public ExceptionSummary handleException(Throwable exception) {
93 return handle("", exception);
94 }
95
96 private ExceptionSummary handle(String message, Throwable exception) {
97 String reference = getReference(exception);
98
99 List<ExceptionSummary> children = null;
100
101 if (exception instanceof ProjectBuildingException) {
102 List<ProjectBuildingResult> results = ((ProjectBuildingException) exception).getResults();
103
104 children = new ArrayList<>();
105
106 for (ProjectBuildingResult result : results) {
107 ExceptionSummary child = handle(result);
108 if (child != null) {
109 children.add(child);
110 }
111 }
112
113 message = "The build could not read " + children.size() + " project" + (children.size() == 1 ? "" : "s");
114 } else {
115 message = getMessage(message, exception);
116 }
117
118 return new ExceptionSummary(exception, message, reference, children);
119 }
120
121 private ExceptionSummary handle(ProjectBuildingResult result) {
122 List<ExceptionSummary> children = new ArrayList<>();
123
124 for (ModelProblem problem : result.getProblems()) {
125 ExceptionSummary child = handle(problem, result.getProjectId());
126 if (child != null) {
127 children.add(child);
128 }
129 }
130
131 if (children.isEmpty()) {
132 return null;
133 }
134
135 String message = System.lineSeparator()
136 + "The project " + (result.getProjectId().isEmpty() ? "" : result.getProjectId() + " ")
137 + "(" + result.getPomFile() + ") has "
138 + children.size() + " error" + (children.size() == 1 ? "" : "s");
139
140 return new ExceptionSummary(null, message, null, children);
141 }
142
143 private ExceptionSummary handle(ModelProblem problem, String projectId) {
144 if (ModelProblem.Severity.ERROR.compareTo(problem.getSeverity()) >= 0) {
145 String message = problem.getMessage();
146
147 String location = ModelProblemUtils.formatLocation(problem, projectId);
148
149 if (!location.isEmpty()) {
150 message += " @ " + location;
151 }
152
153 return handle(message, problem.getException());
154 } else {
155 return null;
156 }
157 }
158
159 private String getReference(Throwable exception) {
160 String reference = "";
161
162 if (exception != null) {
163 if (exception instanceof MojoExecutionException) {
164 reference = MojoExecutionException.class.getSimpleName();
165
166 Throwable cause = exception.getCause();
167 if (cause instanceof IOException) {
168 cause = cause.getCause();
169 if (cause instanceof ConnectException) {
170 reference = ConnectException.class.getSimpleName();
171 }
172 }
173 } else if (exception instanceof MojoFailureException) {
174 reference = MojoFailureException.class.getSimpleName();
175 } else if (exception instanceof LinkageError) {
176 reference = LinkageError.class.getSimpleName();
177 } else if (exception instanceof PluginExecutionException) {
178 Throwable cause = exception.getCause();
179
180 if (cause instanceof PluginContainerException) {
181 Throwable cause2 = cause.getCause();
182
183 if (cause2 instanceof NoClassDefFoundError) {
184 String message = cause2.getMessage();
185 if (message != null && message.contains("org/sonatype/aether/")) {
186 reference = "AetherClassNotFound";
187 }
188 }
189 }
190
191 if (StringUtils.isEmpty(reference)) {
192 reference = getReference(cause);
193 }
194
195 if (StringUtils.isEmpty(reference)) {
196 reference = exception.getClass().getSimpleName();
197 }
198 } else if (exception instanceof LifecycleExecutionException) {
199 reference = getReference(exception.getCause());
200 } else if (isNoteworthyException(exception)) {
201 reference = exception.getClass().getSimpleName();
202 }
203 }
204
205 if (StringUtils.isNotEmpty(reference) && !reference.startsWith("http:")) {
206 reference = "http://cwiki.apache.org/confluence/display/MAVEN/" + reference;
207 }
208
209 return reference;
210 }
211
212 private boolean isNoteworthyException(Throwable exception) {
213 if (exception == null) {
214 return false;
215 } else if (exception instanceof Error) {
216 return true;
217 } else if (exception instanceof RuntimeException) {
218 return false;
219 } else {
220 return !exception.getClass().getName().startsWith("java");
221 }
222 }
223
224 private String getMessage(String message, Throwable exception) {
225 String fullMessage = (message != null) ? message : "";
226
227 for (Throwable t = exception; t != null; t = t.getCause()) {
228 String exceptionMessage = t.getMessage();
229
230 if (t instanceof AbstractMojoExecutionException) {
231 String longMessage = ((AbstractMojoExecutionException) t).getLongMessage();
232 if (StringUtils.isNotEmpty(longMessage)) {
233 if (StringUtils.isEmpty(exceptionMessage) || longMessage.contains(exceptionMessage)) {
234 exceptionMessage = longMessage;
235 } else if (!exceptionMessage.contains(longMessage)) {
236 exceptionMessage = join(exceptionMessage, System.lineSeparator() + longMessage);
237 }
238 }
239 }
240
241 if (StringUtils.isEmpty(exceptionMessage)) {
242 exceptionMessage = t.getClass().getSimpleName();
243 }
244
245 if (t instanceof UnknownHostException && !fullMessage.contains("host")) {
246 fullMessage = join(fullMessage, "Unknown host " + exceptionMessage);
247 } else if (!fullMessage.contains(exceptionMessage)) {
248 fullMessage = join(fullMessage, exceptionMessage);
249 }
250 }
251
252 return fullMessage.trim();
253 }
254
255 private String join(String message1, String message2) {
256 String message = "";
257
258 if (StringUtils.isNotEmpty(message1)) {
259 message = message1.trim();
260 }
261
262 if (StringUtils.isNotEmpty(message2)) {
263 if (StringUtils.isNotEmpty(message)) {
264 if (message.endsWith(".") || message.endsWith("!") || message.endsWith(":")) {
265 message += " ";
266 } else {
267 message += ": ";
268 }
269 }
270
271 message += message2;
272 }
273
274 return message;
275 }
276 }