View Javadoc

1   package com.aragost.javahg;
2   
3   import com.aragost.javahg.internals.AbstractCommand;
4   
5   /**
6    * Exception thrown when an attempt it made to execute a command that
7    * the underlying Mercurial command server doesn't know.
8    * <p>
9    * Since Mercurial from time to time introduce new commands (e.g.
10   * phase, graft), JavaHg implements these. But if you attempt to use
11   * them against an older version of Mercurial you get this exception.
12   * 
13   */
14  public class UnknownCommandException extends RuntimeException {
15  
16      private static final long serialVersionUID = 1L;
17  
18      // TODO Exceptions are Serializable, AbstractCommand not. So we
19      // should not have it as a non-transient field.
20      private final AbstractCommand command;
21  
22      public UnknownCommandException(AbstractCommand cmd) {
23          super(createMessage(cmd));
24          this.command = cmd;
25      }
26  
27      private static String createMessage(AbstractCommand cmd) {
28          HgVersion hgVersion = getHgVersion(cmd);
29          return "Mercurial command '" + cmd.getCommandName() + "' is not supported by Mercurial version: "
30                  + hgVersion.getVersionString();
31      }
32  
33      private static HgVersion getHgVersion(AbstractCommand cmd) {
34          HgVersion hgVersion = HgVersion.unknown();
35          try {
36              hgVersion = cmd.getRepository().getHgVersion();
37          } catch (RuntimeException e) {
38              // Something went wrong, but ignore this so we can create
39              // this exception
40          }
41          return hgVersion;
42      }
43  
44      public AbstractCommand getCommand() {
45          return command;
46      }
47  
48  }