Skip to content

Commit

Permalink
Improved error handling/logging; now can log errors like:
Browse files Browse the repository at this point in the history
500 INTERNAL_SERVER_ERROR "json-to-biopax failed"; nested exception is java.lang.AssertionError: Unsupported json shcema (expected 'interactions' array)
  • Loading branch information
IgorRodchenkov committed Mar 31, 2024
1 parent 8342467 commit c912daa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/main/java/factoid/converter/FactoidToBiopax.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class FactoidToBiopax {

//todo: it's a work-in-progress; currently cannot do a round-trip data conversion, e.g. biopax->(biofactoid)json->biopax...

private static final Map<String, ControlType> CONTROL_TYPE_MAP = createControlTypeMap();

private TemplateModel model;
Expand Down Expand Up @@ -59,11 +61,14 @@ public void addToModel(Reader contentReader) {
* @param docTemplate
*/
public void addToModel(JsonObject docTemplate) {

// quick checks if we can handle this json data
if(!docTemplate.has("interactions") || !docTemplate.get("interactions").isJsonArray()) {
throw new AssertionError("Unsupported json shcema (expected 'interactions' array)");
}

JsonArray intnTemplates = docTemplate.get("interactions").getAsJsonArray();

Iterator<JsonElement> it = intnTemplates.iterator();

while (it.hasNext()) {
JsonObject template = (JsonObject) it.next();
String typeStr = template.get("type").getAsString();
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/factoid/web/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public String jsonToBiopax(
converter.addToModel(body);
} catch (IllegalStateException | JsonSyntaxException | JsonIOException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.toString());
} catch (Throwable e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "json-to-biopax failed", e);
}

// Convert the model to biopax string
Expand All @@ -69,8 +69,8 @@ public String jsonToSbgn(
ByteArrayOutputStream baos = new ByteArrayOutputStream();
converter.writeSBGN(model, baos);
return baos.toString(StandardCharsets.UTF_8.name());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.toString());
} catch (Throwable e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "json-to-sbgn failed", e);
}
}

Expand All @@ -90,8 +90,8 @@ public String biopaxToSbgn(
ByteArrayOutputStream baos = new ByteArrayOutputStream();
converter.writeSBGN(model, baos);
return baos.toString(StandardCharsets.UTF_8.name());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.toString());
} catch (Throwable e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "biopax-to-sbgn failed", e);
}
}

Expand All @@ -109,8 +109,8 @@ public String biopaxToFactoid(
return converter.convert(model).toString();
} catch (IllegalStateException | JsonSyntaxException | JsonIOException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.toString());
} catch (Throwable e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "biopax-to-json failed", e);
}
}

Expand All @@ -129,8 +129,8 @@ public String biopaxUrlToFactoid(
return converter.convert(model).toString();
} catch (IllegalStateException | JsonSyntaxException | JsonIOException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.toString());
} catch (Throwable e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "biopax-url-to-json failed", e);
}
}

Expand All @@ -156,7 +156,7 @@ private String getContentFromUrl(String url) {
e.printStackTrace();
}

String body = writer.toString();
return body;
String body = writer.toString();
return body;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ springdoc.swagger-ui.enabled=true
#springdoc.packagesToScan=?
#springdoc.pathsToMatch=?

spring.mvc.log-resolved-exception=true


0 comments on commit c912daa

Please sign in to comment.