8000 Throw more meaningful NotFoundException when appropriate · kanglingv/libgit2sharp@23f97d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23f97d9

Browse files
jamillnulltoken
authored andcommitted
Throw more meaningful NotFoundException when appropriate
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 29e6a3c commit 23f97d9

File tree

8 files changed

+20
-19
lines changed

8 files changed

+20
-19
lines changed

LibGit2Sharp.Tests/BlameFixture.cs

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

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

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void CreatingBranchWithUnknownNamedTargetThrows()
254254
string path = SandboxBareTestRepo();
255255
using (var repo = new Repository(path))
256256
{
257-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
257+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", "my_old_branch"));
258258
}
259259
}
260260

@@ -264,8 +264,8 @@ public void CreatingBranchWithUnknownShaTargetThrows()
264264
string path = SandboxBareTestRepo();
265265
using (var repo = new Repository(path))
266266
{
267-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
268-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
267+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha));
268+
Assert.Throws<NotFoundException>(() => repo.Branches.Add("my_new_branch", Constants.UnknownSha.Substring(0, 7)));
269269
}
270270
}
271271

@@ -729,7 +729,7 @@ public void SetTrackedBranchForUnreasolvableRemoteThrows()
729729

730730
Branch trackedBranch = repo.Branches[trackedBranchName];
731731

732-
Assert.Throws<LibGit2SharpException>(() => repo.Branches.Update(branch,
732+
Assert.Throws<NotFoundException>(() => repo.Branches.Update(branch,
733733
b => b.TrackedBranch = trackedBranch.CanonicalName));
734734
}
735735
}

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void CheckingOutANonExistingBranchThrows()
383383
string path = SandboxBareTestRepo();
384384
using (var repo = new Repository(path))
385385
{
386-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("i-do-not-exist"));
386+
Assert.Throws<NotFoundException>(() => repo.Checkout("i-do-not-exist"));
387387
}
388388
}
389389

@@ -895,7 +895,7 @@ public void CheckoutLowerCasedHeadThrows()
895895
string path = SandboxStandardTestRepo();
896896
using (var repo = new Repository(path))
897897
{
898-
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("head"));
898+
Assert.Throws<NotFoundException>(() => repo.Checkout("head"));
899899
}
900900
}
901901

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public void QueryingTheCommitHistoryWithUnknownShaOrInvalidEntryPointThrows()
107107
string path = SandboxBareTestRepo();
108108
using (var repo = new Repository(path))
109109
{
110-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
111-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
110+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = Constants.UnknownSha }).Count());
111+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = "refs/heads/deadbeef" }).Count());
112112
Assert.Throws<ArgumentNullException>(() => repo.Commits.QueryBy(new CommitFilter { Since = null }).Count());
113113
}
114114
}
@@ -121,8 +121,8 @@ public void QueryingTheCommitHistoryFromACorruptedReferenceThrows()
121121
{
122122
CreateCorruptedDeadBeefHead(repo.Info.Path);
123123

124-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
125-
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
124+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Branches["deadbeef"] }).Count());
125+
Assert.Throws<NotFoundException>(() => repo.Commits.QueryBy(new CommitFilter { Since = repo.Refs["refs/heads/deadbeef"] }).Count());
126126
}
127127
}
128128

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 = SandboxBareTestRepo();
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

@@ -610,7 +610,7 @@ public void UpdatingADirectReferenceTargetWithARevparsePointingAtAnUnknownObject
610610
string path = SandboxBareTestRepo();
611611
using (var repo = new Repository(path))
612612
{
613-
Assert.Throws<LibGit2SharpException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
613+
Assert.Throws<NotFoundException>(() => repo.Refs.UpdateTarget(repo.Refs["refs/heads/master"], "refs/heads/nope"));
614614
}
615615
}
616616

LibGit2Sharp.Tests/ResetHeadFixture.cs

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

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public void CreatingATagForAnUnknowReferenceThrows()
258258
string path = SandboxBareTestRepo();
259259
using (var repo = new Repository(path))
260260
{
261-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
261+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", "aaaaaaaaaaa"));
262262
}
263263
}
264264

@@ -269,7 +269,7 @@ public void CreatingATagForAnUnknowObjectIdThrows()
269269
string path = SandboxBareTestRepo();
270270
using (var repo = new Repository(path))
271271
{
272-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
272+
Assert.Throws<NotFoundException>(() => repo.ApplyTag("mytagnorev", Constants.UnknownSha));
273273
}
274274
}
275275

@@ -507,7 +507,7 @@ public void AddTagWithNotExistingTargetThrows()
507507
string path = SandboxBareTestRepo();
508508
using (var repo = new Repository(path))
509509
{
510-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
510+
Assert.Throws<NotFoundException>(() => repo.Tags.Add("test_tag", Constants.UnknownSha, signatureTim, "message"));
511511
}
512512
}
513513

@@ -623,7 +623,7 @@ public void RemovingAnUnknownTagShouldFail()
623623
string path = SandboxBareTestRepo();
624624
using (var repo = new Repository(path))
625625
{
626-
Assert.Throws<LibGit2SharpException>(() => repo.Tags.Remove("unknown-tag"));
626+
Assert.Throws<NotFoundException>(() => repo.Tags.Remove("unknown-tag"));
627627
}
628628
}
629629

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)
0