@@ -104,7 +104,7 @@ private JsonNode requestJsonNode(
104104 Request request = buildRequest (method , path , queryParams , body , multipartBody );
105105 try (Response response = httpClient .newCall (request ).execute ()) {
106106 if (!response .isSuccessful ()) {
107- throw buildApiException (readBodyText (response ), response . code () );
107+ throw buildApiException (readBodyText (response ), response );
108108 }
109109
110110 ResponseBody responseBody = response .body ();
@@ -131,7 +131,7 @@ private byte[] requestBytes(String method, String path, Object body) {
131131 Request request = buildRequest (method , path , null , body , null );
132132 try (Response response = httpClient .newCall (request ).execute ()) {
133133 if (!response .isSuccessful ()) {
134- throw buildApiException (readBodyText (response ), response . code () );
134+ throw buildApiException (readBodyText (response ), response );
135135 }
136136 ResponseBody responseBody = response .body ();
137137 return responseBody == null ? new byte [0 ] : responseBody .bytes ();
@@ -146,7 +146,7 @@ private InputStream requestStream(String method, String path, Object body) {
146146 Response response = httpClient .newCall (request ).execute ();
147147 if (!response .isSuccessful ()) {
148148 try {
149- throw buildApiException (readBodyText (response ), response . code () );
149+ throw buildApiException (readBodyText (response ), response );
150150 } finally {
151151 response .close ();
152152 }
@@ -207,10 +207,13 @@ private static JsonNode firstDefined(JsonNode node, String... keys) {
207207 return null ;
208208 }
209209
210- private FacturapiException buildApiException (String bodyText , int statusCode ) {
210+ private FacturapiException buildApiException (String bodyText , Response response ) {
211+ int statusCode = response .code ();
211212 int resolvedStatus = statusCode ;
212213 Object errorCode = null ;
213214 String errorPath = null ;
215+ String errorLocation = null ;
216+ JsonNode errors = null ;
214217 String message = "Request failed with status " + statusCode ;
215218
216219 if (bodyText != null && !bodyText .isBlank ()) {
@@ -261,6 +264,16 @@ private FacturapiException buildApiException(String bodyText, int statusCode) {
261264 errorPath = pathNode .asText ();
262265 }
263266
267+ JsonNode locationNode = firstDefined (root , "location" );
268+ if (locationNode != null && locationNode .isTextual ()) {
269+ errorLocation = locationNode .asText ();
270+ }
271+
272+ JsonNode errorsNode = firstDefined (root , "errors" );
273+ if (errorsNode != null && errorsNode .isArray ()) {
274+ errors = errorsNode ;
275+ }
276+
264277 if (firstDefined (root , "message" , "error" , "detail" ) == null ) {
265278 message = bodyText ;
266279 }
@@ -270,7 +283,16 @@ private FacturapiException buildApiException(String bodyText, int statusCode) {
270283 }
271284 }
272285
273- return new FacturapiException (message , resolvedStatus , errorCode , errorPath );
286+ return new FacturapiException (
287+ message ,
288+ resolvedStatus ,
289+ errorCode ,
290+ errorPath ,
291+ errorLocation ,
292+ errors ,
293+ response .header ("x-facturapi-log-id" ),
294+ response .headers ().toMultimap ()
295+ );
274296 }
275297
276298 private static String readBodyText (Response response ) throws IOException {
0 commit comments