Introduce CallFailedRuntimeException in client
This commit is contained in:
committed by
Michael Vorburger ⛑️
parent
27bebff4ac
commit
1af69db132
+73
@@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.fineract.client.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown by {@link Calls#ok(Call)} when {@link Call}s fail.
|
||||||
|
*
|
||||||
|
* @author Michael Vorburger.ch
|
||||||
|
*/
|
||||||
|
public class CallFailedRuntimeException extends RuntimeException {
|
||||||
|
|
||||||
|
private final Call<?> call;
|
||||||
|
private final Response<?> response;
|
||||||
|
|
||||||
|
public <T> CallFailedRuntimeException(Call<T> call, Throwable t) {
|
||||||
|
super("HTTP failed: " + call.request().toString(), t);
|
||||||
|
this.call = call;
|
||||||
|
this.response = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> CallFailedRuntimeException(Call<T> call, Response<T> response) {
|
||||||
|
super(message(call, response));
|
||||||
|
this.call = call;
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String message(Call<?> call, Response<?> response) {
|
||||||
|
StringBuilder sb = new StringBuilder("HTTP failed: " + call.request() + "; " + response);
|
||||||
|
if (response.message() != null && !response.message().isEmpty()) {
|
||||||
|
sb.append("; message: " + response.message());
|
||||||
|
}
|
||||||
|
String errorBody;
|
||||||
|
try {
|
||||||
|
errorBody = response.errorBody().string();
|
||||||
|
if (errorBody != null) {
|
||||||
|
sb.append("; errorBody: " + errorBody);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Ignore.
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> Call<T> getCall() {
|
||||||
|
return (Call<T>) call;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> Response<T> getResponse() {
|
||||||
|
return (Response<T>) response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,15 +37,20 @@ public final class Calls {
|
|||||||
* @param call
|
* @param call
|
||||||
* the Call to execute
|
* the Call to execute
|
||||||
* @return the body of the successful call (never null)
|
* @return the body of the successful call (never null)
|
||||||
* @throws IOException
|
* @throws CallFailedRuntimeException
|
||||||
* thrown either if a problem occurred talking to the server, or the HTTP response code was not
|
* thrown either if a problem occurred talking to the server, or the HTTP response code was not
|
||||||
* [200..300) successful
|
* [200..300) successful
|
||||||
*/
|
*/
|
||||||
public static <T> T ok(Call<T> call) throws IOException {
|
public static <T> T ok(Call<T> call) throws CallFailedRuntimeException {
|
||||||
Response<T> response = call.execute();
|
Response<T> response;
|
||||||
|
try {
|
||||||
|
response = call.execute();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CallFailedRuntimeException(call, e);
|
||||||
|
}
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
return response.body();
|
return response.body();
|
||||||
}
|
}
|
||||||
throw new IOException("HTTP failed: " + call.request() + "; " + response);
|
throw new CallFailedRuntimeException(call, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -22,7 +22,6 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import static org.apache.fineract.client.util.Calls.ok;
|
import static org.apache.fineract.client.util.Calls.ok;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import org.apache.fineract.client.util.FineractClient;
|
import org.apache.fineract.client.util.FineractClient;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -34,13 +33,13 @@ import org.junit.jupiter.api.Test;
|
|||||||
*/
|
*/
|
||||||
public class FineractClientTest {
|
public class FineractClientTest {
|
||||||
|
|
||||||
void checkClients(FineractClient fineract) throws IOException {
|
void checkClients(FineractClient fineract) {
|
||||||
assertThat(ok(fineract.clients.retrieveAll20(null, null, null, null, null, null, null, null, 0, 100, null, null, null))
|
assertThat(ok(fineract.clients.retrieveAll20(null, null, null, null, null, null, null, null, 0, 100, null, null, null))
|
||||||
.getTotalFilteredRecords()).isAtLeast(3);
|
.getTotalFilteredRecords()).isAtLeast(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRetrieveAllClientsFromFineractDev() throws IOException {
|
void testRetrieveAllClientsFromFineractDev() {
|
||||||
FineractClient fineract = FineractClient.builder().baseURL("https://demo.fineract.dev/fineract-provider/api/v1/").tenant("default")
|
FineractClient fineract = FineractClient.builder().baseURL("https://demo.fineract.dev/fineract-provider/api/v1/").tenant("default")
|
||||||
.basicAuth("mifos", "password").build();
|
.basicAuth("mifos", "password").build();
|
||||||
checkClients(fineract);
|
checkClients(fineract);
|
||||||
@@ -48,7 +47,7 @@ public class FineractClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled // TODO remove Disabled once https://issues.apache.org/jira/browse/FINERACT-1209 is fixed
|
@Disabled // TODO remove Disabled once https://issues.apache.org/jira/browse/FINERACT-1209 is fixed
|
||||||
void testRetrieveAllClientsFromLocalhostWithInsecureSelfSignedCert() throws IOException {
|
void testRetrieveAllClientsFromLocalhostWithInsecureSelfSignedCert() {
|
||||||
FineractClient fineract = FineractClient.builder().baseURL("https://localhost:8443/fineract-provider/api/v1/").tenant("default")
|
FineractClient fineract = FineractClient.builder().baseURL("https://localhost:8443/fineract-provider/api/v1/").tenant("default")
|
||||||
.basicAuth("mifos", "password").insecure(true).build();
|
.basicAuth("mifos", "password").insecure(true).build();
|
||||||
checkClients(fineract);
|
checkClients(fineract);
|
||||||
@@ -64,14 +63,14 @@ public class FineractClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled // TODO remove Ignore once https://issues.apache.org/jira/browse/FINERACT-1221 is fixed
|
@Disabled // TODO remove Ignore once https://issues.apache.org/jira/browse/FINERACT-1221 is fixed
|
||||||
void testInvalidOperations() throws IOException {
|
void testInvalidOperations() {
|
||||||
FineractClient.Builder builder = FineractClient.builder().baseURL("http://test/").tenant("default").basicAuth("mifos", "password");
|
FineractClient.Builder builder = FineractClient.builder().baseURL("http://test/").tenant("default").basicAuth("mifos", "password");
|
||||||
builder.getRetrofitBuilder().validateEagerly(true); // see FINERACT-1221
|
builder.getRetrofitBuilder().validateEagerly(true); // see FINERACT-1221
|
||||||
builder.build();
|
builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFineractClientBuilder() throws IOException {
|
void testFineractClientBuilder() {
|
||||||
assertThrows(IllegalStateException.class, () -> {
|
assertThrows(IllegalStateException.class, () -> {
|
||||||
FineractClient.builder().build();
|
FineractClient.builder().build();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user