changeset 531:95c2f43008bd

Throw specific exception when checksum calculation fails
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 23 Jan 2013 18:03:13 +0100
parents 0f6fa88e2162
children 688c1ab113bb
files src/org/tmatesoft/hg/core/HgCloneCommand.java src/org/tmatesoft/hg/core/HgRevisionIntegrityException.java src/org/tmatesoft/hg/repo/HgInvalidFileException.java
diffstat 3 files changed, 51 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgCloneCommand.java	Wed Jan 23 17:46:12 2013 +0100
+++ b/src/org/tmatesoft/hg/core/HgCloneCommand.java	Wed Jan 23 18:03:13 2013 +0100
@@ -298,8 +298,8 @@
 				byte[] calculated = dh.sha1(p1, p2, content).asBinary();
 				final Nodeid node = ge.node();
 				if (!node.equalsTo(calculated)) {
-					// TODO post-1.0 custom exception ChecksumCalculationFailed?
-					throw new HgInvalidStateException(String.format("Checksum failed: expected %s, calculated %s. File %s", node, calculated, filename));
+					String m = String.format("Checksum failed: expected %s, calculated %s. File %s", node, calculated, filename);
+					throw new HgRevisionIntegrityException(m, null, new File(hgDir, filename));
 				}
 				revlogHeader.nodeid(node);
 				if (collectChangelogIndexes) {
@@ -348,7 +348,7 @@
 				prevRevContent = new ByteArrayDataAccess(content);
 			} catch (IOException ex) {
 				String m = String.format("Failed to write revision %s of file %s", ge.node().shortNotation(), filename);
-				throw new HgInvalidControlFileException(m, ex, new File(filename));
+				throw new HgInvalidControlFileException(m, ex, new File(hgDir, filename));
 			}
 			return cancelException == null;
 		}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/core/HgRevisionIntegrityException.java	Wed Jan 23 18:03:13 2013 +0100
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2013 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.core;
+
+import java.io.File;
+
+import org.tmatesoft.hg.repo.HgInvalidControlFileException;
+import org.tmatesoft.hg.repo.HgInvalidFileException;
+
+/**
+ * Thrown to indicate integrity issues with a revision, namely, when digest (SHA1) over revision data
+ * doesn't match revision id.
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+@SuppressWarnings("serial")
+public class HgRevisionIntegrityException extends HgInvalidControlFileException {
+	
+	/**
+	 * See {@link HgInvalidFileException#HgInvalidFileException(String, Throwable, File)} for parameters description.
+	 */
+	public HgRevisionIntegrityException(String message, Throwable th, File file) {
+		super(message, th, file);
+	}
+
+}
--- a/src/org/tmatesoft/hg/repo/HgInvalidFileException.java	Wed Jan 23 17:46:12 2013 +0100
+++ b/src/org/tmatesoft/hg/repo/HgInvalidFileException.java	Wed Jan 23 18:03:13 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2012 TMate Software Ltd
+ * Copyright (c) 2011-2013 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,6 +43,12 @@
 		super(message, th);
 	}
 
+	/**
+	 * 
+	 * @param message description of the trouble, may (although should not) be <code>null</code>
+	 * @param th cause, optional
+	 * @param file where the trouble is, may be <code>null</code>, can be altered later with {@link #setFile(File)}
+	 */
 	public HgInvalidFileException(String message, Throwable th, File file) {
 		super(message, th);
 		details.setFile(file); // allows null