1 /* 2 * #%L 3 * JavaHg 4 * %% 5 * Copyright (C) 2011 aragost Trifork ag 6 * %% 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 * #L% 25 */ 26 package com.aragost.javahg.commands; 27 28 import com.aragost.javahg.internals.AbstractCommand; 29 import com.google.common.base.Strings; 30 31 /** 32 * Generic exception thrown when executing a Mercurial command fails. 33 */ 34 public class ExecutionException extends RuntimeException { 35 36 private static final String ABORT_PREFIX = "abort: "; 37 38 private static final long serialVersionUID = 1L; 39 40 // TODO Exceptions are Serializable, AbstractCommand not. So we 41 // should not have it as a non-transient field. 42 private final AbstractCommand command; 43 44 /** 45 * @param message 46 * the exception message 47 * @param command 48 * the command that failed. 49 */ 50 public ExecutionException(AbstractCommand command, String message) { 51 super(message); 52 this.command = command; 53 } 54 55 /** 56 * @param command 57 * the command that failed. 58 */ 59 public ExecutionException(AbstractCommand command) { 60 this(command, generateMessage(command)); 61 } 62 63 /** 64 * @return the command that failed. 65 */ 66 public AbstractCommand getCommand() { 67 return command; 68 } 69 70 /** 71 * Generate generic exception message based on the command. 72 * 73 * @param cmd 74 * @return 75 */ 76 private static String generateMessage(AbstractCommand cmd) { 77 String s = cmd.getErrorString().trim(); 78 if (Strings.isNullOrEmpty(s)) { 79 s = "Error in command '" + cmd.getCommandName() + "' [rc: " + cmd.getReturnCode() + "]"; 80 } 81 if (s.startsWith(ABORT_PREFIX)) { 82 s = s.substring(ABORT_PREFIX.length()); 83 } 84 return s; 85 } 86 87 }