improve exception handling

This commit is contained in:
Sebastian Sdorra
2011-05-12 19:36:10 +02:00
parent 8c2a383b2b
commit 883b9f1a3c
15 changed files with 486 additions and 146 deletions

View File

@@ -33,6 +33,11 @@
package sonia.scm.client;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------
import com.sun.jersey.api.client.Client;
@@ -47,6 +52,67 @@ import com.sun.jersey.api.client.filter.LoggingFilter;
public class ClientUtil
{
/** the logger for ClientUtil */
private static final Logger logger =
LoggerFactory.getLogger(ClientUtil.class);
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param exception
* @param response
*/
public static void appendContent(ScmClientException exception,
ClientResponse response)
{
try
{
exception.setContent(response.getEntity(String.class));
}
catch (Exception ex)
{
logger.warn("could not read content", ex);
}
}
/**
* Method description
*
*
* @param response
* @param expectedStatusCode
*/
public static void checkResponse(ClientResponse response,
int expectedStatusCode)
{
int sc = response.getStatus();
if (sc != expectedStatusCode)
{
sendException(response, sc);
}
}
/**
* Method description
*
*
* @param response
*
*/
public static void checkResponse(ClientResponse response)
{
int sc = response.getStatus();
if (sc >= 300)
{
sendException(response, sc);
}
}
/**
* Method description
*
@@ -97,4 +163,57 @@ public class ClientUtil
return resource;
}
/**
* Method description
*
*
* @param response
* @param sc
*/
public static void sendException(ClientResponse response, int sc)
{
ScmClientException exception = null;
switch (sc)
{
case ScmClientException.SC_UNAUTHORIZED :
exception = new ScmUnauthorizedException();
break;
case ScmClientException.SC_FORBIDDEN :
exception = new ScmForbiddenException();
break;
case ScmClientException.SC_NOTFOUND :
exception = new ScmNotFoundException();
break;
default :
exception = new ScmClientException(sc);
appendContent(exception, response);
}
throw exception;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param response
*
* @return
*/
public static boolean isSuccessfull(ClientResponse response)
{
int status = response.getStatus();
return (status > 200) && (status < 300);
}
}

View File

@@ -95,12 +95,10 @@ public class JerseyClientProvider implements ScmClientProvider
*
* @return
*
* @throws ScmClientException
*/
@Override
public JerseyClientSession createSession(String url, String username,
String password)
throws ScmClientException
{
AssertUtil.assertIsNotEmpty(url);
@@ -153,24 +151,7 @@ public class JerseyClientProvider implements ScmClientProvider
response = resource.get(ClientResponse.class);
}
if (response.getStatus() != 200)
{
String msg =
"server returned ".concat(String.valueOf(response.getStatus()));
if (logger.isWarnEnabled())
{
logger.warn(msg);
}
if (logger.isTraceEnabled())
{
logger.trace("server returned content: {}",
response.getEntity(String.class));
}
throw new ScmClientException(msg);
}
ClientUtil.checkResponse(response);
ScmState state = response.getEntity(ScmState.class);

View File

@@ -83,10 +83,9 @@ public class JerseyClientSession implements ScmClientSession
* Method description
*
*
* @throws IOException
*/
@Override
public void close() throws IOException
public void close()
{
if (logger.isInfoEnabled())
{

View File

@@ -88,26 +88,19 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
try
{
response = resource.post(ClientResponse.class, repository);
ClientUtil.checkResponse(response, 201);
if (response.getStatus() == 201)
{
String url = response.getHeaders().get("Location").get(0);
String url = response.getHeaders().get("Location").get(0);
AssertUtil.assertIsNotEmpty(url);
AssertUtil.assertIsNotEmpty(url);
Repository newRepository = getRepository(url);
Repository newRepository = getRepository(url);
AssertUtil.assertIsNotNull(newRepository);
newRepository.copyProperties(repository);
AssertUtil.assertIsNotNull(newRepository);
newRepository.copyProperties(repository);
// copyProperties does not copy the repository id
repository.setId(newRepository.getId());
}
else
{
// todo errorhandling
}
// copyProperties does not copy the repository id
repository.setId(newRepository.getId());
}
finally
{
@@ -132,12 +125,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
try
{
response = resource.delete(ClientResponse.class);
if (response.getStatus() != 204)
{
// todo errorhandling
}
ClientUtil.checkResponse(response, 204);
}
finally
{
@@ -179,12 +167,7 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
try
{
response = resource.post(ClientResponse.class, repository);
if (response.getStatus() != 204)
{
// todo errorhandling
}
ClientUtil.checkResponse(response, 204);
}
finally
{
@@ -224,17 +207,9 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
try
{
response = resource.get(ClientResponse.class);
if (response.getStatus() == 200)
{
repositories = response.getEntity(new GenericType<List<Repository>>() {}
);
}
else
{
// todo errorhandling
}
ClientUtil.checkResponse(response, 200);
repositories = response.getEntity(new GenericType<List<Repository>>() {}
);
}
finally
{
@@ -274,14 +249,12 @@ public class JerseyRepositoryClientHandler implements RepositoryClientHandler
{
response = resource.get(ClientResponse.class);
if (response.getStatus() == 200)
{
repository = response.getEntity(Repository.class);
}
else
{
int sc = response.getStatus();
// todo errorhandling
if (sc != ScmClientException.SC_NOTFOUND)
{
ClientUtil.checkResponse(response, 200);
repository = response.getEntity(Repository.class);
}
}
finally