Skip to content

Commit 80f2d51

Browse files
authored
Better logging for unparsable error messages (#168)
1 parent f6c5bb0 commit 80f2d51

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/main/java/com/botdetector/http/BotDetectorClient.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -553,29 +553,35 @@ private <T> T processResponse(Gson gson, Response response, Type type) throws IO
553553
private IOException getIOException(Response response)
554554
{
555555
int code = response.code();
556-
if (code == 422)
557-
{
558-
// TODO: Parse actual error info received from FastAPI (details -> loc, msg, ctx, etc.)
559-
return new ValidationException("Error 422 from API, invalid data format");
560-
}
561-
else if (code >= 400 && code < 500)
556+
if (code >= 400 && code < 500)
562557
{
563558
try
564559
{
565-
Map<String, String> map = gson.fromJson(response.body().string(),
566-
new TypeToken<Map<String, String>>()
567-
{
568-
}.getType());
560+
String body = response.body().string();
561+
try
562+
{
563+
Map<String, String> map = gson.fromJson(body,
564+
new TypeToken<Map<String, String>>()
565+
{
566+
}.getType());
569567

570-
// "error" has priority if it exists, else use "detail" (FastAPI)
571-
String error = map.get("error");
572-
if (Strings.isNullOrEmpty(error))
568+
// "error" has priority if it exists, else use "detail" (FastAPI)
569+
String error = map.get("error");
570+
if (Strings.isNullOrEmpty(error))
571+
{
572+
error = map.getOrDefault("detail", "Unknown " + code + " error from API");
573+
}
574+
return new IOException(error);
575+
}
576+
catch (JsonSyntaxException ex)
573577
{
574-
error = map.getOrDefault("detail", "Unknown " + code + " error from API");
578+
// If can't parse, just log the response body
579+
// TODO: Parse actual error info received from FastAPI (details -> loc, msg, ctx, etc.) especially for 422 errors
580+
log.warn("Received HTTP error code " + code + " from API with the following response body:\n" + body);
581+
return new IOException("Error " + code + ", see log for more info");
575582
}
576-
return new IOException(error);
577583
}
578-
catch (IOException | JsonSyntaxException ex)
584+
catch (IOException ex)
579585
{
580586
return new IOException("Error " + code + " with no error info", ex);
581587
}

0 commit comments

Comments
 (0)