Skip to content

Commit

Permalink
Better logging for unparsable error messages (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyborger1 authored Nov 9, 2022
1 parent f6c5bb0 commit 80f2d51
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/main/java/com/botdetector/http/BotDetectorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,29 +553,35 @@ private <T> T processResponse(Gson gson, Response response, Type type) throws IO
private IOException getIOException(Response response)
{
int code = response.code();
if (code == 422)
{
// TODO: Parse actual error info received from FastAPI (details -> loc, msg, ctx, etc.)
return new ValidationException("Error 422 from API, invalid data format");
}
else if (code >= 400 && code < 500)
if (code >= 400 && code < 500)
{
try
{
Map<String, String> map = gson.fromJson(response.body().string(),
new TypeToken<Map<String, String>>()
{
}.getType());
String body = response.body().string();
try
{
Map<String, String> map = gson.fromJson(body,
new TypeToken<Map<String, String>>()
{
}.getType());

// "error" has priority if it exists, else use "detail" (FastAPI)
String error = map.get("error");
if (Strings.isNullOrEmpty(error))
// "error" has priority if it exists, else use "detail" (FastAPI)
String error = map.get("error");
if (Strings.isNullOrEmpty(error))
{
error = map.getOrDefault("detail", "Unknown " + code + " error from API");
}
return new IOException(error);
}
catch (JsonSyntaxException ex)
{
error = map.getOrDefault("detail", "Unknown " + code + " error from API");
// If can't parse, just log the response body
// TODO: Parse actual error info received from FastAPI (details -> loc, msg, ctx, etc.) especially for 422 errors
log.warn("Received HTTP error code " + code + " from API with the following response body:\n" + body);
return new IOException("Error " + code + ", see log for more info");
}
return new IOException(error);
}
catch (IOException | JsonSyntaxException ex)
catch (IOException ex)
{
return new IOException("Error " + code + " with no error info", ex);
}
Expand Down

0 comments on commit 80f2d51

Please sign in to comment.