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.api.services; 20 21 import org.apache.maven.api.annotations.Experimental; 22 23 /** 24 * Base class for all maven exceptions carrying {@link BuilderProblem}s. 25 * 26 * @since 4.0.0 27 */ 28 @Experimental 29 public abstract class MavenBuilderException extends MavenException { 30 31 /** 32 * The collection of problems associated with this exception. 33 */ 34 private final ProblemCollector<BuilderProblem> problems; 35 36 /** 37 * Constructs a new exception with the specified message and cause. 38 * This constructor creates an empty problem collector. 39 * 40 * @param message the detail message 41 * @param cause the cause of this exception 42 */ 43 public MavenBuilderException(String message, Throwable cause) { 44 super(message, cause); 45 problems = ProblemCollector.empty(); 46 } 47 48 /** 49 * Constructs a new exception with the specified message and problems. 50 * The message will be enhanced with details from the problems. 51 * 52 * @param message the detail message 53 * @param problems the collection of problems associated with this exception 54 */ 55 public MavenBuilderException(String message, ProblemCollector<BuilderProblem> problems) { 56 super(buildMessage(message, problems), null); 57 this.problems = problems; 58 } 59 60 /** 61 * Formats message out of problems: problems are sorted (in natural order of {@link BuilderProblem.Severity}) 62 * and then a list is built. These exceptions are usually thrown in "fatal" cases (and usually prevent Maven 63 * from starting), and these exceptions may end up very early on output. 64 * 65 * @param message the base message to enhance 66 * @param problems the collection of problems to include in the message 67 * @return a formatted message including details of all problems 68 */ 69 protected static String buildMessage(String message, ProblemCollector<BuilderProblem> problems) { 70 StringBuilder msg = new StringBuilder(message); 71 problems.problems().forEach(problem -> msg.append("\n * ") 72 .append(problem.getSeverity().name()) 73 .append(": ") 74 .append(problem.getMessage())); 75 return msg.toString(); 76 } 77 78 /** 79 * Returns the problem collector associated with this exception. 80 * 81 * @return the problem collector containing all problems related to this exception 82 */ 83 public ProblemCollector<BuilderProblem> getProblemCollector() { 84 return problems; 85 } 86 }