Skip to content

Commit

Permalink
Handle exceptions during HTTP POST requests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjohnson-kint committed Mar 8, 2017
1 parent 7c393a8 commit 5d872af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Consumer;

public class HttpClient {

Expand All @@ -17,10 +18,14 @@ public class HttpClient {

private final io.vertx.core.http.HttpClient client;
private final URL okapiUrl;
private final Consumer<Throwable> exceptionHandler;

public HttpClient(Vertx vertx, URL okapiUrl) {
public HttpClient(Vertx vertx,
URL okapiUrl,
Consumer<Throwable> exceptionHandler) {
this.client = vertx.createHttpClient();
this.okapiUrl = okapiUrl;
this.exceptionHandler = exceptionHandler;
}

public void post(URL url,
Expand All @@ -38,6 +43,12 @@ public void post(URL url,
request.headers().add(TENANT_HEADER, tenantId);
}

request.setTimeout(3000);

request.exceptionHandler(exception -> {
this.exceptionHandler.accept(exception);
});

if(body != null) {
String encodedBody = Json.encodePrettily(body);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,29 @@ private void create(RoutingContext routingContext) {
return;
}

HttpClient client = new HttpClient(routingContext.vertx(), okapiLocation);
client.post(storageLocation,
routingContext.getBodyAsJson(),
context.getTenantId(), response -> {
response.bodyHandler(buffer -> {
String responseBody = BufferHelper.stringFromBuffer(buffer);

if(response.statusCode() == 201) {
JsonResponse.created(routingContext.response(),
new JsonObject(responseBody));
}
else {
ServerErrorResponse.internalError(routingContext.response(),
String.format("Response From Storage Module: %s: %s",
response.statusCode(), responseBody));
}
});
HttpClient client = new HttpClient(routingContext.vertx(), okapiLocation,
exception -> {
ServerErrorResponse.internalError(routingContext.response(),
String.format("Failed to contact storage module: %s",
exception.toString()));
});

client.post(storageLocation,
routingContext.getBodyAsJson(),
context.getTenantId(), response -> {
response.bodyHandler(buffer -> {
String responseBody = BufferHelper.stringFromBuffer(buffer);

if(response.statusCode() == 201) {
JsonResponse.created(routingContext.response(),
new JsonObject(responseBody));
}
else {
ServerErrorResponse.internalError(routingContext.response(),
String.format("Response From Storage Module: %s: %s",
response.statusCode(), responseBody));
}
});
});
}
}
6 changes: 5 additions & 1 deletion src/test/java/org/folio/circulation/api/LoanAPITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class LoanAPITests {

HttpClient client = APITestSuite.createUsingVertx(
(Vertx vertx) -> new HttpClient(vertx,
APITestSuite.storageUrl()));
APITestSuite.storageUrl(), exception -> {
System.out.println(
String.format("Request to circulation module failed: %s",
exception.toString()));
}));

@Test
public void canCreateALoan()
Expand Down

0 comments on commit 5d872af

Please sign in to comment.