use already exists exceptions always with an appropriate message

This commit is contained in:
Sebastian Sdorra
2016-12-07 22:40:24 +01:00
parent 3709ce7602
commit 64581e1f75
5 changed files with 39 additions and 25 deletions

View File

@@ -115,7 +115,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
if (directory.exists())
{
throw new RepositoryAlreadyExistsException();
throw RepositoryAlreadyExistsException.create(repository);
}
checkPath(directory);
@@ -381,7 +381,7 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
content = Resources.toString(url, Charsets.UTF_8);
}
}
catch (Exception ex)
catch (IOException ex)
{
logger.error("could not read resource", ex);
}
@@ -432,7 +432,9 @@ public abstract class AbstractSimpleRepositoryHandler<C extends SimpleRepository
logger.error("parent path {} is a repository", parent);
}
throw new RepositoryAlreadyExistsException();
StringBuilder buffer = new StringBuilder("repository with name ");
buffer.append(directory.getName()).append(" already exists");
throw new RepositoryAlreadyExistsException(buffer.toString());
}
parent = parent.getParentFile();

View File

@@ -34,12 +34,35 @@
package sonia.scm.repository;
/**
* This {@link Exception} is thrown when trying to create a repository with
* the same name and type already exists.
*
* @author Sebastian Sdorra
*/
public class RepositoryAlreadyExistsException extends RepositoryException
{
/** Field description */
private static final long serialVersionUID = -774929917214137675L;
/**
* Creates a new instance.
*
* @param message exception message
*/
public RepositoryAlreadyExistsException(String message) {
super(message);
}
/**
* Creates a new {@link RepositoryAlreadyExistsException} with an appropriate message.
*
* @param repository repository that already exists
*
* @return new exception with appropriate message
*/
public static RepositoryAlreadyExistsException create(Repository repository){
StringBuilder buffer = new StringBuilder("repository with name ");
buffer.append(repository.getName()).append(" of type ");
buffer.append(repository.getType()).append("already exists");
return new RepositoryAlreadyExistsException(buffer.toString());
}
}

View File

@@ -33,11 +33,8 @@
package sonia.scm.user;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
/**
* This {@link Exception} is thrown when trying to create a user that already exists.
*
* @author Sebastian Sdorra
*/
@@ -50,21 +47,13 @@ public class UserAlreadyExistsException extends UserException
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
* Constructs a new instance.
*
*/
public UserAlreadyExistsException() {}
/**
* Constructs ...
*
*
* @param username
* @param message message of exception
* @since 1.5
*/
public UserAlreadyExistsException(String username)
public UserAlreadyExistsException(String message)
{
super("user \"".concat(Util.nonNull(username)).concat(
"\" allready exists"));
super(message);
}
}

View File

@@ -171,7 +171,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
if (repositoryDAO.contains(repository))
{
throw new RepositoryAlreadyExistsException();
throw RepositoryAlreadyExistsException.create(repository);
}
repository.setId(keyGenerator.createKey());

View File

@@ -159,7 +159,7 @@ public class DefaultUserManager extends AbstractUserManager
if (userDAO.contains(user.getName()))
{
throw new UserAlreadyExistsException(user.getName());
throw new UserAlreadyExistsException(user.getName().concat(" user already exists"));
}
AssertUtil.assertIsValid(user);