Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git.validators; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.server.validators.ValidationException; 19 : import java.util.List; 20 : 21 : /** 22 : * Exception thrown when a Git commit fails validations. Gerrit supports a wide range of validations 23 : * (for example it validates any commits pushed to NoteDb refs for format compliance or allows to 24 : * enforce commit message lengths to not exceed a certain length). 25 : */ 26 : public class CommitValidationException extends ValidationException { 27 : private static final long serialVersionUID = 1L; 28 : private final ImmutableList<CommitValidationMessage> messages; 29 : 30 : public CommitValidationException(String reason, CommitValidationMessage message) { 31 1 : super(reason); 32 1 : this.messages = ImmutableList.of(message); 33 1 : } 34 : 35 : public CommitValidationException(String reason, CommitValidationMessage message, Throwable why) { 36 0 : super(reason, why); 37 0 : this.messages = ImmutableList.of(message); 38 0 : } 39 : 40 : public CommitValidationException(String reason, List<CommitValidationMessage> messages) { 41 15 : super(reason); 42 15 : this.messages = ImmutableList.copyOf(messages); 43 15 : } 44 : 45 : public CommitValidationException(String reason) { 46 10 : super(reason); 47 10 : this.messages = ImmutableList.of(); 48 10 : } 49 : 50 : public CommitValidationException(String reason, Throwable why) { 51 0 : super(reason, why); 52 0 : this.messages = ImmutableList.of(); 53 0 : } 54 : 55 : /** Returns all validation messages individually. */ 56 : public ImmutableList<CommitValidationMessage> getMessages() { 57 15 : return messages; 58 : } 59 : 60 : /** Returns all validation as a single, formatted string. */ 61 : public String getFullMessage() { 62 15 : StringBuilder sb = new StringBuilder(getMessage()); 63 15 : if (!messages.isEmpty()) { 64 10 : sb.append(':'); 65 10 : for (CommitValidationMessage msg : messages) { 66 10 : sb.append("\n ").append(msg.getMessage()); 67 10 : } 68 : } 69 15 : return sb.toString(); 70 : } 71 : }