View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  - test projects for each of these
43  - how to categorize the problems so that the id of the problem can be match to a page with descriptive help and the test
44    project
45  - nice little sample projects that could be run in the core as well as integration tests
46  
47  All Possible Errors
48  - invalid lifecycle phase (maybe same as bad CLI param, though you were talking about embedder too)
49  - <module> specified is not found
50  - malformed settings
51  - malformed POM
52  - local repository not writable
53  - remote repositories not available
54  - artifact metadata missing
55  - extension metadata missing
56  - extension artifact missing
57  - artifact metadata retrieval problem
58  - version range violation
59  - circular dependency
60  - artifact missing
61  - artifact retrieval exception
62  - md5 checksum doesn't match for local artifact, need to redownload this
63  - POM doesn't exist for a goal that requires one
64  - parent POM missing (in both the repository + relative path)
65  - component not found
66  
67  Plugins:
68  - plugin metadata missing
69  - plugin metadata retrieval problem
70  - plugin artifact missing
71  - plugin artifact retrieval problem
72  - plugin dependency metadata missing
73  - plugin dependency metadata retrieval problem
74  - plugin configuration problem
75  - plugin execution failure due to something that is know to possibly go wrong (like compilation failure)
76  - plugin execution error due to something that is not expected to go wrong (the compiler executable missing)
77  - asking to use a plugin for which you do not have a version defined - tools to easily select versions
78  - goal not found in a plugin (probably could list the ones that are)
79  
80   */
81  
82  // PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
83  // CycleDetectedInPluginGraphException;
84  
85  /**
86   * Transform an exception into useful end-user message.
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 }