2525import java .io .IOException ;
2626import okhttp3 .Response ;
2727import okhttp3 .ResponseBody ;
28- import org .apache .http .HttpEntity ;
29- import org .apache .http .HttpStatus ;
30- import org .apache .http .StatusLine ;
31- import org .apache .http .client .methods .CloseableHttpResponse ;
32- import org .apache .http .util .EntityUtils ;
3328
3429/** General exception class for all exceptions originating from the GenAI API side. */
3530public class ApiException extends BaseException {
@@ -52,31 +47,6 @@ public ApiException(int code, String status, String message) {
5247 this .message = message ;
5348 }
5449
55- /**
56- * Throws an ApiException from the response if the response is not a OK status.
57- *
58- * @param response The response from the API call.
59- * @deprecated Use {@link #throwFromResponse(Response)} instead.
60- */
61- @ ExcludeFromGeneratedCoverageReport
62- @ Deprecated
63- public static void throwFromResponse (CloseableHttpResponse response ) {
64- StatusLine statusLine = response .getStatusLine ();
65- int code = statusLine .getStatusCode ();
66- if (code == HttpStatus .SC_OK ) {
67- return ;
68- }
69- String status = statusLine .getReasonPhrase ();
70- String message = getErrorMessageFromResponse (response );
71- if (code >= 400 && code < 500 ) { // Client errors.
72- throw new ClientException (code , status , message );
73- } else if (code >= 500 && code < 600 ) { // Server errors.
74- throw new ServerException (code , status , message );
75- } else {
76- throw new ApiException (code , status , message );
77- }
78- }
79-
8050 /**
8151 * Throws an ApiException from the response if the response is not a OK status.
8252 *
@@ -99,33 +69,6 @@ public static void throwFromResponse(Response response) {
9969 }
10070 }
10171
102- /**
103- * Returns the error message from the response, if no error or error message is not found, then
104- * returns an empty string.
105- */
106- @ ExcludeFromGeneratedCoverageReport
107- @ Deprecated
108- static String getErrorMessageFromResponse (CloseableHttpResponse response ) {
109- HttpEntity entity = response .getEntity ();
110- try {
111- String responseBody = EntityUtils .toString (entity );
112- if (responseBody == null || responseBody .isEmpty ()) {
113- return "" ;
114- }
115- ObjectMapper mapper = new ObjectMapper ();
116- JsonNode errorNode = mapper .readTree (responseBody ).get ("error" );
117- if (errorNode != null && errorNode .isObject ()) {
118- JsonNode messageNode = errorNode .get ("message" );
119- if (messageNode != null && messageNode .isTextual ()) {
120- return messageNode .asText ();
121- }
122- }
123- return "" ;
124- } catch (IOException ignored ) {
125- return "" ;
126- }
127- }
128-
12972 /**
13073 * Returns the error message from the response, if no error or error message is not found, then
13174 * returns an empty string.
0 commit comments