From c912daa5eaa8ef382c8ec6dd5899f3a38974f8ed Mon Sep 17 00:00:00 2001 From: IgorRodchenkov Date: Sun, 31 Mar 2024 15:24:00 -0400 Subject: [PATCH] Improved error handling/logging; now can log errors like: 500 INTERNAL_SERVER_ERROR "json-to-biopax failed"; nested exception is java.lang.AssertionError: Unsupported json shcema (expected 'interactions' array) --- .../factoid/converter/FactoidToBiopax.java | 11 ++++++--- src/main/java/factoid/web/Controller.java | 24 +++++++++---------- .../resources/config/application.properties | 3 +++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/factoid/converter/FactoidToBiopax.java b/src/main/java/factoid/converter/FactoidToBiopax.java index 7ace64d..5c27129 100644 --- a/src/main/java/factoid/converter/FactoidToBiopax.java +++ b/src/main/java/factoid/converter/FactoidToBiopax.java @@ -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 CONTROL_TYPE_MAP = createControlTypeMap(); private TemplateModel model; @@ -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 it = intnTemplates.iterator(); - while (it.hasNext()) { JsonObject template = (JsonObject) it.next(); String typeStr = template.get("type").getAsString(); diff --git a/src/main/java/factoid/web/Controller.java b/src/main/java/factoid/web/Controller.java index bbd498d..d2f7921 100644 --- a/src/main/java/factoid/web/Controller.java +++ b/src/main/java/factoid/web/Controller.java @@ -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 @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -156,7 +156,7 @@ private String getContentFromUrl(String url) { e.printStackTrace(); } - String body = writer.toString(); - return body; + String body = writer.toString(); + return body; } } diff --git a/src/main/resources/config/application.properties b/src/main/resources/config/application.properties index fd82513..2d949cc 100644 --- a/src/main/resources/config/application.properties +++ b/src/main/resources/config/application.properties @@ -12,3 +12,6 @@ springdoc.swagger-ui.enabled=true #springdoc.packagesToScan=? #springdoc.pathsToMatch=? +spring.mvc.log-resolved-exception=true + +