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
|
||||
* the Call to execute
|
||||
* @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
|
||||
* [200..300) successful
|
||||
*/
|
||||
public static <T> T ok(Call<T> call) throws IOException {
|
||||
Response<T> response = call.execute();
|
||||
public static <T> T ok(Call<T> call) throws CallFailedRuntimeException {
|
||||
Response<T> response;
|
||||
try {
|
||||
response = call.execute();
|
||||
} catch (IOException e) {
|
||||
throw new CallFailedRuntimeException(call, e);
|
||||
}
|
||||
if (response.isSuccessful()) {
|
||||
return response.body();
|
||||
}
|
||||
throw new IOException("HTTP failed: " + call.request() + "; " + response);
|
||||
throw new CallFailedRuntimeException(call, response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user