Introduce default error object with context for not found exceptions

This commit is contained in:
René Pfeuffer
2018-10-18 13:12:16 +02:00
parent a56aeca8d2
commit b74fb814b8
61 changed files with 395 additions and 1553 deletions

View File

@@ -50,13 +50,12 @@ import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.GitSubModuleParser;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SubRepository;
import sonia.scm.util.Util;
@@ -103,7 +102,7 @@ public class GitBrowseCommand extends AbstractGitCommand
@Override
@SuppressWarnings("unchecked")
public BrowserResult getBrowserResult(BrowseCommandRequest request)
throws IOException, RevisionNotFoundException {
throws IOException {
logger.debug("try to create browse result for {}", request);
BrowserResult result;
@@ -157,7 +156,7 @@ public class GitBrowseCommand extends AbstractGitCommand
*/
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
throws IOException, RevisionNotFoundException {
throws IOException {
FileObject file;
try
@@ -267,7 +266,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId)
throws IOException, RevisionNotFoundException {
throws IOException {
BrowserResult result = null;
RevWalk revWalk = null;
TreeWalk treeWalk = null;
@@ -364,7 +363,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private Map<String,
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
ObjectId revision)
throws IOException, RevisionNotFoundException {
throws IOException {
if (logger.isDebugEnabled())
{
logger.debug("read submodules of {} at {}", repository.getName(),
@@ -378,7 +377,7 @@ public class GitBrowseCommand extends AbstractGitCommand
PATH_MODULES, baos);
subRepositories = GitSubModuleParser.parse(baos.toString());
}
catch (PathNotFoundException ex)
catch (NotFoundException ex)
{
logger.trace("could not find .gitmodules", ex);
subRepositories = Collections.EMPTY_MAP;
@@ -389,7 +388,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private SubRepository getSubRepository(org.eclipse.jgit.lib.Repository repo,
ObjectId revId, String path)
throws IOException, RevisionNotFoundException {
throws IOException {
Map<String, SubRepository> subRepositories = subrepositoryCache.get(revId);
if (subRepositories == null)

View File

@@ -44,9 +44,8 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.util.Util;
import java.io.Closeable;
@@ -65,7 +64,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
}
@Override
public void getCatResult(CatCommandRequest request, OutputStream output) throws IOException, PathNotFoundException, RevisionNotFoundException {
public void getCatResult(CatCommandRequest request, OutputStream output) throws IOException {
logger.debug("try to read content for {}", request);
try (ClosableObjectLoaderContainer closableObjectLoaderContainer = getLoader(request)) {
closableObjectLoaderContainer.objectLoader.copyTo(output);
@@ -73,24 +72,24 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
}
@Override
public InputStream getCatResultStream(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
public InputStream getCatResultStream(CatCommandRequest request) throws IOException {
logger.debug("try to read content for {}", request);
return new InputStreamWrapper(getLoader(request));
}
void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path, OutputStream output) throws IOException, PathNotFoundException, RevisionNotFoundException {
void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path, OutputStream output) throws IOException {
try (ClosableObjectLoaderContainer closableObjectLoaderContainer = getLoader(repo, revId, path)) {
closableObjectLoaderContainer.objectLoader.copyTo(output);
}
}
private ClosableObjectLoaderContainer getLoader(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
private ClosableObjectLoaderContainer getLoader(CatCommandRequest request) throws IOException {
org.eclipse.jgit.lib.Repository repo = open();
ObjectId revId = getCommitOrDefault(repo, request.getRevision());
return getLoader(repo, revId, request.getPath());
}
private ClosableObjectLoaderContainer getLoader(Repository repo, ObjectId revId, String path) throws IOException, PathNotFoundException, RevisionNotFoundException {
private ClosableObjectLoaderContainer getLoader(Repository repo, ObjectId revId, String path) throws IOException {
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.setRecursive(Util.nonNull(path).contains("/"));
@@ -102,7 +101,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
try {
entry = revWalk.parseCommit(revId);
} catch (MissingObjectException e) {
throw new RevisionNotFoundException(revId.getName());
throw NotFoundException.notFound("Revision", revId.getName()).in(repository).build();
}
RevTree revTree = entry.getTree();
@@ -120,7 +119,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
return new ClosableObjectLoaderContainer(loader, treeWalk, revWalk);
} else {
throw new PathNotFoundException(path);
throw NotFoundException.notFound("Path", path).in("Revision", revId.getName()).in(repository).build();
}
}

View File

@@ -48,12 +48,12 @@ import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.GitChangesetConverter;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.util.IOUtil;
import java.io.IOException;
@@ -85,7 +85,6 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
*
* @param context
* @param repository
* @param repositoryDirectory
*/
GitLogCommand(GitContext context, sonia.scm.repository.Repository repository)
{
@@ -162,7 +161,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
*/
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getChangesets(LogCommandRequest request) throws RevisionNotFoundException {
public ChangesetPagingResult getChangesets(LogCommandRequest request) {
if (logger.isDebugEnabled()) {
logger.debug("fetch changesets for request: {}", request);
}
@@ -249,7 +248,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
}
catch (MissingObjectException e)
{
throw new RevisionNotFoundException(e.getObjectId().name());
throw NotFoundException.notFound("Revision", e.getObjectId().getName()).in(repository).build();
}
catch (Exception ex)
{

View File

@@ -39,7 +39,6 @@ import org.junit.Test;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
import java.util.List;
@@ -63,7 +62,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
* Test browse command with default branch.
*/
@Test
public void testDefaultBranch() throws IOException, RevisionNotFoundException {
public void testDefaultBranch() throws IOException {
// without default branch, the repository head should be used
BrowserResult result = createCommand().getBrowserResult(new BrowseCommandRequest());
assertNotNull(result);
@@ -93,7 +92,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testBrowse() throws IOException, RevisionNotFoundException {
public void testBrowse() throws IOException {
BrowserResult result =
createCommand().getBrowserResult(new BrowseCommandRequest());
@@ -134,7 +133,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testBrowseSubDirectory() throws IOException, RevisionNotFoundException {
public void testBrowseSubDirectory() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setPath("c");
@@ -181,7 +180,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testRecusive() throws IOException, RevisionNotFoundException {
public void testRecusive() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setRecursive(true);

View File

@@ -32,10 +32,13 @@
package sonia.scm.repository.spi;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -51,9 +54,12 @@ import static org.junit.Assert.assertEquals;
* @author Sebastian Sdorra
*/
public class GitCatCommandTest extends AbstractGitCommandTestBase {
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testDefaultBranch() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testDefaultBranch() throws IOException {
// without default branch, the repository head should be used
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
@@ -66,7 +72,7 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void testCat() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testCat() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
@@ -75,32 +81,58 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void testSimpleCat() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testSimpleCat() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("b.txt");
assertEquals("b", execute(request));
}
@Test(expected = PathNotFoundException.class)
public void testUnknownFile() throws IOException, PathNotFoundException, RevisionNotFoundException {
@Test
public void testUnknownFile() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("unknown");
execute(request);
}
@Test(expected = RevisionNotFoundException.class)
public void testUnknownRevision() throws IOException, PathNotFoundException, RevisionNotFoundException {
CatCommandRequest request = new CatCommandRequest();
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for path");
}
@Override
public boolean matches(Object item) {
return "Path".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
request.setRevision("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
request.setPath("a.txt");
execute(request);
}
@Test
public void testSimpleStream() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testUnknownRevision() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setRevision("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
request.setPath("a.txt");
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for revision");
}
@Override
public boolean matches(Object item) {
return "Revision".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
execute(request);
}
@Test
public void testSimpleStream() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("b.txt");
@@ -113,7 +145,7 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
catResultStream.close();
}
private String execute(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
private String execute(CatCommandRequest request) throws IOException {
String content = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();