Skip to content

Commit cdc4afc

Browse files
jamillnulltoken
authored andcommitted
Introduce NotFoundException
This change is to throw more specific exceptions in several cases when possible. If an object cannot be found, throw a more specific NotFoundException.
1 parent b24a00c commit cdc4afc

8 files changed

Lines changed: 20 additions & 19 deletions

File tree

LibGit2Sharp.Tests/BlameFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void CanBlameFromADifferentCommit()
3434
using (var repo = new Repository(MergedTestRepoWorkingDirPath))
3535
{
3636
// File doesn't exist at HEAD
37-
Assert.Throws<LibGit2SharpException>(() => repo.Blame("ancestor-only.txt"));
37+
Assert.Throws<NotFoundException>(() => repo.Blame("ancestor-only.txt"));
3838

3939
var blame = repo.Blame("ancestor-only.txt", new BlameOptions { StartingAt = "9107b30" });
4040
Assert.Equal(1, blame.Count());

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public void CreatingBranchWithUnknownNamedTargetThrows()
252252
{
253253
using (var repo = new Repository(BareTestRepoPath))
254254
{
255-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
255+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
256256
}
257257
}
258258

@@ -261,8 +261,8 @@ public void CreatingBranchWithUnknownShaTargetThrows()
261261
{
262262
using (var repo = new Repository(BareTestRepoPath))
263263
{
264-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
265-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
264+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
265+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
266266
}
267267
}
268268

@@ -707,7 +707,7 @@ public void SetTrackedBranchForUnreasolvableRemoteThrows()
707707

708708
Branch trackedBranch = repo.Branches[trackedBranchName];
709709

710-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Update(branch,
710+
Assert.Throws<NotFoundException>(() => repo.Branches.Update(branch,
711711
b => b.TrackedBranch = trackedBranch.CanonicalName));
712712
}
713713
}

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public void CheckingOutANonExistingBranchThrows()
381381
{
382382
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
383383
{
384-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("i-do-not-exist"));
384+
Assert.Throws<NotFoundException>(() => repo.Checkout("i-do-not-exist"));
385385
}
386386
}
387387

@@ -891,7 +891,7 @@ public void CheckoutLowerCasedHeadThrows()
891891
{
892892
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
893893
{
894-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("head"));
894+
Assert.Throws<NotFoundException>(() => repo.Checkout("head"));
895895
}
896896
}
897897

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void QueryingTheCommitHistoryWithUnknownShaOrInvalidEntryPointThrows()
102102
{
103103
using (var repo = new Repository(BareTestRepoPath))
104104
{
105-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
106-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
105+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
106+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
107107
Assert.Throws<ArgumentNullException>(() => repo.Commits.QueryBy(new CommitFilter { Since = null }).Count());
108108
}
109109
}
@@ -116,8 +116,8 @@ public void QueryingTheCommitHistoryFromACorruptedReferenceThrows()
116116
{
117117
CreateCorruptedDeadBeefHead(repo.Info.Path);
118118

119-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
120-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
119+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
120+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
121121
}
122122
}
123123

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void CreatingADirectReferenceWithARevparseSpecPointingAtAnUnknownObjectFa
7373
string path = CloneBareTestRepo();
7474
using (var repo = new Repository(path))
7575
{
76-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.Add(name, "master^42"));
76+
Assert.Throws<NotFoundException>(() => repo.Refs.Add(name, "master^42"));
7777
}
7878
}
7979

@@ -596,7 +596,7 @@ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObject
596596
{
597597
using (var repo = new Repository(BareTestRepoPath))
598598
{
599-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
599+
Assert.Throws<NotFoundException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
600600
}
601601
}
602602

LibGit2Sharp.Tests/ResetHeadFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void ResettingWithBadParamsThrows()
7070
Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetMode.Soft, (string)null));
7171
Assert.Throws<ArgumentNullException>(() => repo.Reset(ResetMode.Soft, (Commit)null));
7272
Assert.Throws<ArgumentException>(() => repo.Reset(ResetMode.Soft, ""));
73-
Assert.Throws<LibGit2SharpException>(() => repo.Reset(ResetMode.Soft, Constants.UnknownSha));
73+
Assert.Throws<NotFoundException>(() => repo.Reset(ResetMode.Soft, Constants.UnknownSha));
7474
Assert.Throws<InvalidSpecificationException>(() => repo.Reset(ResetMode.Soft, repo.Head.Tip.Tree.Sha));
7575
}
7676
}

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public void CreatingATagForAnUnknowReferenceThrows()
257257
{
258258
using (var repo = new Repository(BareTestRepoPath))
259259
{
260-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
260+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
261261
}
262262
}
263263

@@ -267,7 +267,7 @@ public void CreatingATagForAnUnknowObjectIdThrows()
267267
{
268268
using (var repo = new Repository(BareTestRepoPath))
269269
{
270-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
270+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
271271
}
272272
}
273273

@@ -497,7 +497,7 @@ public void AddTagWithNotExistingTargetThrows()
497497
{
498498
using (var repo = new Repository(BareTestRepoPath))
499499
{
500-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
500+
Assert.Throws<NotFoundException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
501501
}
502502
}
503503

@@ -608,7 +608,7 @@ public void RemovingAnUnknownTagShouldFail()
608608
{
609609
using (var repo = new Repository(BareTestRepoPath))
610610
{
611-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Remove("unknown-tag"));
611+
Assert.Throws<NotFoundException>(() => repo.Tags.Remove("unknown-tag"));
612612
}
613613
}
614614

LibGit2Sharp/Core/Ensure.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitE
100100
{ GitErrorCode.NonFastForward, (m, r, c) => new NonFastForwardException(m, r, c) },
101101
{ GitErrorCode.MergeConflict, (m, r, c) => new MergeConflictException(m, r, c) },
102102
{ GitErrorCode.LockedFile, (m, r, c) => new LockedFileException(m, r, c) },
103+
{ GitErrorCode.NotFound, (m, r, c) => new NotFoundException(m, r, c) },
103104
};
104105

105106
private static void HandleError(int result)
@@ -215,7 +216,7 @@ public static void GitObjectIsNotNull(GitObject gitObject, string identifier)
215216
}
216217
else
217218
{
218-
exceptionBuilder = m => new LibGit2SharpException(m);
219+
exceptionBuilder = m => new NotFoundException(m);
219220
}
220221

221222
GitObjectIsNotNull(gitObject, identifier, exceptionBuilder);

0 commit comments

Comments
 (0)