Use original error detail message on conflict errors

pull/30/head
Richard Körber 2016-07-07 00:11:02 +02:00
parent cb5a853715
commit 7eb2fe5945
3 changed files with 18 additions and 12 deletions

View File

@ -105,16 +105,11 @@ public class RegistrationBuilder {
} }
int rc = conn.sendSignedRequest(session.resourceUri(Resource.NEW_REG), claims, session); int rc = conn.sendSignedRequest(session.resourceUri(Resource.NEW_REG), claims, session);
if (rc != HttpURLConnection.HTTP_CREATED && rc != HttpURLConnection.HTTP_CONFLICT) { if (rc != HttpURLConnection.HTTP_CREATED) {
conn.throwAcmeException(); conn.throwAcmeException();
} }
URI location = conn.getLocation(); URI location = conn.getLocation();
if (rc == HttpURLConnection.HTTP_CONFLICT) {
throw new AcmeConflictException("Account is already registered", location);
}
URI tos = conn.getLink("terms-of-service"); URI tos = conn.getLink("terms-of-service");
return new Registration(session, location, tos); return new Registration(session, location, tos);

View File

@ -40,6 +40,7 @@ import org.jose4j.jwk.PublicJsonWebKey;
import org.jose4j.jws.JsonWebSignature; import org.jose4j.jws.JsonWebSignature;
import org.jose4j.lang.JoseException; import org.jose4j.lang.JoseException;
import org.shredzone.acme4j.Session; import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.exception.AcmeConflictException;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.exception.AcmeRateLimitExceededException; import org.shredzone.acme4j.exception.AcmeRateLimitExceededException;
@ -335,6 +336,10 @@ public class DefaultConnection implements Connection {
detail = "general problem"; detail = "general problem";
} }
if (conn.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new AcmeConflictException(detail, getLocation());
}
if (type == null) { if (type == null) {
throw new AcmeException(detail); throw new AcmeException(detail);
} }

View File

@ -170,7 +170,7 @@ public class DefaultConnectionTest {
*/ */
@Test @Test
public void testGetLink() throws Exception { public void testGetLink() throws Exception {
Map<String, List<String>> headers = new HashMap<String, List<String>>(); Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", Arrays.asList("application/json")); headers.put("Content-Type", Arrays.asList("application/json"));
headers.put("Location", Arrays.asList("https://example.com/acme/reg/asdf")); headers.put("Location", Arrays.asList("https://example.com/acme/reg/asdf"));
headers.put("Link", Arrays.asList( headers.put("Link", Arrays.asList(
@ -267,7 +267,7 @@ public class DefaultConnectionTest {
} }
verify(mockUrlConnection, atLeastOnce()).getHeaderField("Content-Type"); verify(mockUrlConnection, atLeastOnce()).getHeaderField("Content-Type");
verify(mockUrlConnection).getResponseCode(); verify(mockUrlConnection, atLeastOnce()).getResponseCode();
verify(mockUrlConnection).getErrorStream(); verify(mockUrlConnection).getErrorStream();
verifyNoMoreInteractions(mockUrlConnection); verifyNoMoreInteractions(mockUrlConnection);
} }
@ -276,14 +276,16 @@ public class DefaultConnectionTest {
* Test if an {@link AcmeServerException} is thrown on another problem. * Test if an {@link AcmeServerException} is thrown on another problem.
*/ */
@Test @Test
public void testOtherThrowException() { public void testOtherThrowException() throws IOException {
when(mockUrlConnection.getHeaderField("Content-Type")) when(mockUrlConnection.getHeaderField("Content-Type"))
.thenReturn("application/problem+json"); .thenReturn("application/problem+json");
when(mockUrlConnection.getResponseCode())
.thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) { try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) {
@Override @Override
public Map<String,Object> readJsonResponse() { public Map<String,Object> readJsonResponse() {
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<>();
result.put("type", "urn:zombie:error:apocalypse"); result.put("type", "urn:zombie:error:apocalypse");
result.put("detail", "Zombie apocalypse in progress"); result.put("detail", "Zombie apocalypse in progress");
return result; return result;
@ -301,6 +303,7 @@ public class DefaultConnectionTest {
} }
verify(mockUrlConnection).getHeaderField("Content-Type"); verify(mockUrlConnection).getHeaderField("Content-Type");
verify(mockUrlConnection, atLeastOnce()).getResponseCode();
verifyNoMoreInteractions(mockUrlConnection); verifyNoMoreInteractions(mockUrlConnection);
} }
@ -308,14 +311,16 @@ public class DefaultConnectionTest {
* Test if an {@link AcmeException} is thrown if there is no error type. * Test if an {@link AcmeException} is thrown if there is no error type.
*/ */
@Test @Test
public void testNoTypeThrowException() { public void testNoTypeThrowException() throws IOException {
when(mockUrlConnection.getHeaderField("Content-Type")) when(mockUrlConnection.getHeaderField("Content-Type"))
.thenReturn("application/problem+json"); .thenReturn("application/problem+json");
when(mockUrlConnection.getResponseCode())
.thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) { try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) {
@Override @Override
public Map<String,Object> readJsonResponse() { public Map<String,Object> readJsonResponse() {
return new HashMap<String, Object>(); return new HashMap<>();
}; };
}) { }) {
conn.conn = mockUrlConnection; conn.conn = mockUrlConnection;
@ -328,6 +333,7 @@ public class DefaultConnectionTest {
} }
verify(mockUrlConnection).getHeaderField("Content-Type"); verify(mockUrlConnection).getHeaderField("Content-Type");
verify(mockUrlConnection, atLeastOnce()).getResponseCode();
verifyNoMoreInteractions(mockUrlConnection); verifyNoMoreInteractions(mockUrlConnection);
} }