Remove special RepositoryNotFoundException

This commit is contained in:
René Pfeuffer
2018-10-18 16:08:49 +02:00
parent b74fb814b8
commit bf1effb9c0
21 changed files with 56 additions and 134 deletions

View File

@@ -1,5 +1,6 @@
package sonia.scm;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import java.util.Collections;
@@ -25,14 +26,20 @@ public class NotFoundException extends RuntimeException {
this.context = context;
}
public static NotFoundExceptionBuilder notFound(Repository repository) {
return new NotFoundExceptionBuilder().in(repository);
}
public static NotFoundExceptionBuilder notFound(NamespaceAndName namespaceAndName) {
return new NotFoundExceptionBuilder().in(namespaceAndName);
}
public static NotFoundExceptionBuilder notFound(Class type, String id) {
NotFoundExceptionBuilder builder = new NotFoundExceptionBuilder();
return builder.in(type, id);
return new NotFoundExceptionBuilder().in(type, id);
}
public static NotFoundExceptionBuilder notFound(String type, String id) {
NotFoundExceptionBuilder builder = new NotFoundExceptionBuilder();
return builder.in(type, id);
return new NotFoundExceptionBuilder().in(type, id);
}
public List<ContextEntry> getContext() {
@@ -50,17 +57,20 @@ public class NotFoundException extends RuntimeException {
private final List<ContextEntry> context = new LinkedList<>();
public NotFoundExceptionBuilder in(Repository repository) {
this.in(Repository.class, repository.getNamespaceAndName().logString());
return this;
return in(repository.getNamespaceAndName());
}
public NotFoundExceptionBuilder in(NamespaceAndName namespaceAndName) {
return this.in(Repository.class, namespaceAndName.logString());
}
public NotFoundExceptionBuilder in(Class type, String id) {
this.context.add(new ContextEntry(type, id));
context.add(new ContextEntry(type, id));
return this;
}
public NotFoundExceptionBuilder in(String type, String id) {
this.context.add(new ContextEntry(type, id));
context.add(new ContextEntry(type, id));
return this;
}

View File

@@ -1,68 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
import sonia.scm.NotFoundException;
/**
* Signals that the specified {@link Repository} could be found.
*
* @author Sebastian Sdorra
* @since 1.6
*/
public class RepositoryNotFoundException extends NotFoundException
{
private static final long serialVersionUID = -6583078808900520166L;
private static final String TYPE_REPOSITORY = "Repository";
//~--- constructors ---------------------------------------------------------
/**
* Constructs a new {@link RepositoryNotFoundException} with null as its
* error detail message.
*
*/
public RepositoryNotFoundException(Repository repository) {
super(Repository.class, repository.getNamespaceAndName().logString());
}
public RepositoryNotFoundException(String repositoryId) {
super(TYPE_REPOSITORY, repositoryId);
}
public RepositoryNotFoundException(NamespaceAndName namespaceAndName) {
super(Repository.class, namespaceAndName.logString());
}
}

View File

@@ -38,13 +38,13 @@ package sonia.scm.repository.api;
import com.github.legman.ReferenceType;
import com.github.legman.Subscribe;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.HandlerEventType;
import sonia.scm.NotFoundException;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.config.ScmConfiguration;
@@ -57,7 +57,6 @@ import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKeyPredicate;
import sonia.scm.repository.RepositoryEvent;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.spi.RepositoryServiceProvider;
import sonia.scm.repository.spi.RepositoryServiceResolver;
@@ -161,7 +160,7 @@ public final class RepositoryServiceFactory
* @return a implementation of RepositoryService
* for the given type of repository
*
* @throws RepositoryNotFoundException if no repository
* @throws NotFoundException if no repository
* with the given id is available
* @throws RepositoryServiceNotFoundException if no repository service
* implementation for this kind of repository is available
@@ -170,7 +169,6 @@ public final class RepositoryServiceFactory
* for that repository
*/
public RepositoryService create(NamespaceAndName namespaceAndName)
throws RepositoryNotFoundException
{
Preconditions.checkArgument(namespaceAndName != null,
"a non empty namespace and name is required");
@@ -179,7 +177,7 @@ public final class RepositoryServiceFactory
if (repository == null)
{
throw new RepositoryNotFoundException(namespaceAndName);
throw NotFoundException.notFound(namespaceAndName).build();
}
return create(repository);

View File

@@ -35,12 +35,12 @@ package sonia.scm.repository.spi;
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryHookEvent;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.HookContext;
import sonia.scm.repository.api.HookContextFactory;
@@ -71,18 +71,18 @@ public final class HookEventFacade
//~--- methods --------------------------------------------------------------
public HookEventHandler handle(String id) throws RepositoryNotFoundException {
public HookEventHandler handle(String id) {
return handle(repositoryManagerProvider.get().get(id));
}
public HookEventHandler handle(NamespaceAndName namespaceAndName) throws RepositoryNotFoundException {
public HookEventHandler handle(NamespaceAndName namespaceAndName) {
return handle(repositoryManagerProvider.get().get(namespaceAndName));
}
public HookEventHandler handle(Repository repository) throws RepositoryNotFoundException {
public HookEventHandler handle(Repository repository) {
if (repository == null)
{
throw new RepositoryNotFoundException(repository);
throw NotFoundException.notFound(repository).build();
}
return new HookEventHandler(repositoryManagerProvider.get(),

View File

@@ -44,11 +44,11 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.HgContext;
import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.RepositoryHookType;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryUtil;
import sonia.scm.repository.api.HgHookMessage;
import sonia.scm.repository.api.HgHookMessage.Severity;
@@ -275,17 +275,11 @@ public class HgHookCallbackServlet extends HttpServlet
printMessages(response, context);
}
catch (RepositoryNotFoundException ex)
catch (NotFoundException ex)
{
if (logger.isErrorEnabled())
{
logger.error("could not find repository with id {}", id);
logger.error(ex.getMessage());
if (logger.isTraceEnabled())
{
logger.trace("repository not found", ex);
}
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}

View File

@@ -11,7 +11,6 @@ import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.api.CommandNotSupportedException;
import sonia.scm.repository.api.RepositoryService;
@@ -78,7 +77,7 @@ public class BranchRootResource {
.build();
} catch (CommandNotSupportedException ex) {
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (RepositoryNotFoundException e) {
} catch (NotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@@ -98,7 +97,7 @@ public class BranchRootResource {
@PathParam("name") String name,
@PathParam("branch") String branchName,
@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws Exception {
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
boolean branchExists = repositoryService.getBranchesCommand()
.getBranches()
@@ -151,8 +150,6 @@ public class BranchRootResource {
return Response.ok(branchCollectionToDtoMapper.map(namespace, name, branches.getBranches())).build();
} catch (CommandNotSupportedException ex) {
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (RepositoryNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
}

View File

@@ -9,7 +9,6 @@ import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;

View File

@@ -8,7 +8,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.IOUtil;

View File

@@ -10,7 +10,6 @@ import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.web.VndMediaType;

View File

@@ -11,7 +11,6 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Permission;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.web.VndMediaType;
@@ -131,7 +130,7 @@ public class PermissionRootResource {
@Produces(VndMediaType.PERMISSION)
@TypeHint(PermissionDto.class)
@Path("")
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws RepositoryNotFoundException {
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) {
Repository repository = load(namespace, name);
RepositoryPermissions.permissionRead(repository).check();
return Response.ok(permissionCollectionToDtoMapper.map(repository)).build();
@@ -237,12 +236,12 @@ public class PermissionRootResource {
* @param namespace the repository namespace
* @param name the repository name
* @return the repository if the user is permitted
* @throws RepositoryNotFoundException if the repository does not exists
* @throws NotFoundException if the repository does not exists
*/
private Repository load(String namespace, String name) throws RepositoryNotFoundException {
private Repository load(String namespace, String name) {
NamespaceAndName namespaceAndName = new NamespaceAndName(namespace, name);
return Optional.ofNullable(manager.get(namespaceAndName))
.orElseThrow(() -> new RepositoryNotFoundException(namespaceAndName));
.orElseThrow(() -> NotFoundException.notFound(namespaceAndName).build());
}
/**

View File

@@ -2,7 +2,6 @@ package sonia.scm.api.v2.resources;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.BrowseCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;

View File

@@ -6,7 +6,6 @@ import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.Tag;
import sonia.scm.repository.Tags;

View File

@@ -198,7 +198,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
}
@Override
public void refresh(Repository repository) throws RepositoryNotFoundException {
public void refresh(Repository repository) {
AssertUtil.assertIsNotNull(repository);
RepositoryPermissions.read(repository).check();
@@ -207,7 +207,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
if (fresh != null) {
fresh.copyProperties(repository);
} else {
throw new RepositoryNotFoundException(repository);
throw NotFoundException.notFound(repository).build();
}
}

View File

@@ -61,7 +61,7 @@ public final class HealthChecker {
Repository repository = repositoryManager.get(id);
if (repository == null) {
throw new RepositoryNotFoundException(id);
throw new NotFoundException(Repository.class, id);
}
doCheck(repository);

View File

@@ -4,11 +4,11 @@ import com.google.inject.Inject;
import com.google.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import sonia.scm.NotFoundException;
import sonia.scm.PushStateDispatcher;
import sonia.scm.filter.WebElement;
import sonia.scm.repository.DefaultRepositoryProvider;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.HttpScmProtocol;
@@ -71,8 +71,8 @@ public class HttpProtocolServlet extends HttpServlet {
requestProvider.get().setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repositoryService.getRepository());
HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
protocol.serve(req, resp, getServletConfig());
} catch (RepositoryNotFoundException e) {
log.debug("Repository not found for namespace and name {}", namespaceAndName, e);
} catch (NotFoundException e) {
log.debug(e.getMessage());
resp.setStatus(HttpStatus.SC_NOT_FOUND);
}
}

View File

@@ -10,7 +10,6 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.CatCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -58,7 +57,7 @@ public class ContentResourceTest {
when(catCommand.setRevision(REV)).thenReturn(catCommand);
// defaults for unknown things
doThrow(new RepositoryNotFoundException("x")).when(repositoryServiceFactory).create(not(eq(existingNamespaceAndName)));
doThrow(new NotFoundException("Test", "r")).when(repositoryServiceFactory).create(not(eq(existingNamespaceAndName)));
doThrow(new NotFoundException("Test", "X")).when(catCommand).getStream(any());
}

View File

@@ -21,7 +21,6 @@ import sonia.scm.NotFoundException;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -107,7 +106,7 @@ public class DiffResourceTest extends RepositoryTestBase {
}
@Test
public void shouldGet404OnMissingRepository() throws URISyntaxException, RepositoryNotFoundException {
public void shouldGet404OnMissingRepository() throws URISyntaxException {
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(DIFF_URL + "revision")

View File

@@ -26,7 +26,6 @@ import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Person;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.LogCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -133,7 +132,7 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
@Test
public void shouldGet404OnMissingRepository() throws URISyntaxException, RepositoryNotFoundException {
public void shouldGet404OnMissingRepository() throws URISyntaxException {
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(FILE_HISTORY_URL + "revision/a.txt")

View File

@@ -10,10 +10,10 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.BrowseCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -81,8 +81,8 @@ public class SourceRootResourceTest extends RepositoryTestBase {
}
@Test
public void shouldReturn404IfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new RepositoryNotFoundException("abc"));
public void shouldReturn404IfRepoNotFound() throws URISyntaxException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new NotFoundException("Test", "a"));
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources");
MockHttpResponse response = new MockHttpResponse();
@@ -109,8 +109,8 @@ public class SourceRootResourceTest extends RepositoryTestBase {
}
@Test
public void shouldGet404ForSingleFileIfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new RepositoryNotFoundException("abc"));
public void shouldGet404ForSingleFileIfRepoNotFound() throws URISyntaxException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new NotFoundException("Test", "a"));
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources/revision/fileabc");
MockHttpResponse response = new MockHttpResponse();

View File

@@ -353,7 +353,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
manager.refresh(heartOfGold);
}
@Test(expected = RepositoryNotFoundException.class)
@Test(expected = NotFoundException.class)
public void testRefreshNotFound(){
manager.refresh(createRepositoryWithId());
}
@@ -383,7 +383,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
RepositoryManager repoManager = createRepositoryManager(false);
Repository repository = spy(createTestRepository());
repository.setName("Testrepo");
((DefaultRepositoryManager) repoManager).create(repository);
repoManager.create(repository);
assertEquals("default_namespace", repository.getNamespace());
}

View File

@@ -4,11 +4,11 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import sonia.scm.NotFoundException;
import sonia.scm.PushStateDispatcher;
import sonia.scm.repository.DefaultRepositoryProvider;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.HttpScmProtocol;
@@ -58,12 +58,12 @@ public class HttpProtocolServletTest {
private HttpScmProtocol protocol;
@Before
public void init() throws RepositoryNotFoundException {
public void init() {
initMocks(this);
when(userAgentParser.parse(request)).thenReturn(userAgent);
when(userAgent.isBrowser()).thenReturn(false);
NamespaceAndName existingRepo = new NamespaceAndName("space", "repo");
when(serviceFactory.create(not(eq(existingRepo)))).thenThrow(new RepositoryNotFoundException("x"));
when(serviceFactory.create(not(eq(existingRepo)))).thenThrow(new NotFoundException("Test", "a"));
when(serviceFactory.create(existingRepo)).thenReturn(repositoryService);
when(requestProvider.get()).thenReturn(httpServletRequest);
}
@@ -97,7 +97,7 @@ public class HttpProtocolServletTest {
}
@Test
public void shouldDelegateToProvider() throws RepositoryNotFoundException, IOException, ServletException {
public void shouldDelegateToProvider() throws IOException, ServletException {
when(request.getPathInfo()).thenReturn("/space/name");
NamespaceAndName namespaceAndName = new NamespaceAndName("space", "name");
doReturn(repositoryService).when(serviceFactory).create(namespaceAndName);