From ac3623fb56c77a76a6c30f49650eac0f4367010c Mon Sep 17 00:00:00 2001 From: Ian Young <ian@iay.org.uk> Date: Sat, 12 May 2018 21:41:07 +0100 Subject: [PATCH] Pass back a deeper cause when exceptions happen --- .../iay/incommon/validator/api/ApiException.java | 16 ++++++++++++++++ .../validator/api/ValidatorsApiController.java | 10 ++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/uk/org/iay/incommon/validator/api/ApiException.java b/src/main/java/uk/org/iay/incommon/validator/api/ApiException.java index 6a76629..0b99137 100644 --- a/src/main/java/uk/org/iay/incommon/validator/api/ApiException.java +++ b/src/main/java/uk/org/iay/incommon/validator/api/ApiException.java @@ -46,6 +46,19 @@ public ApiException(final HttpStatus stat, final String msg) { when = DateTime.now(); } + /** + * Constructor. + * + * @param stat HTTP status + * @param msg message + * @param cause the {@link Throwable} that caused this exception + */ + public ApiException(final HttpStatus stat, final String msg, final Throwable cause) { + super(msg, cause); + status = stat; + when = DateTime.now(); + } + /** * Returns the wrapped {@link HttpStatus} value. * @@ -68,6 +81,9 @@ public Map<String, Object> toMap() { m.put("message", getMessage()); m.put("exception", getClass().getName()); m.put("timestamp", when); + if (getCause() != null) { + m.put("cause", getCause().toString()); + } return m; } } diff --git a/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java b/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java index a353368..3b984b2 100644 --- a/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java +++ b/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java @@ -133,8 +133,9 @@ public ResponseEntity<List<Status>> validate( final Document doc = parserPool.parse(new StringReader(metadata)); item = new DOMElementItem(doc); } catch (final XMLParserException ex) { - LOG.info("XLMParserException: {}", ex); - throw new ApiException(HttpStatus.BAD_REQUEST, "XMLParserException: " + ex.getMessage()); + LOG.info("XLMParserException: {}", ex.getMessage()); + throw new ApiException(HttpStatus.BAD_REQUEST, + "XMLParserException: " + ex.getMessage(), ex.getCause()); } // Form the item collection. @@ -146,8 +147,9 @@ public ResponseEntity<List<Status>> validate( try { pipeline.execute(items); } catch (final PipelineProcessingException ex) { - LOG.info("Pipeline failed: {}", ex); - throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, "Pipeline failed: " + ex.getMessage()); + LOG.info("Pipeline failed: {}", ex.getMessage()); + throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, + "Pipeline failed: " + ex.getMessage(), ex.getCause()); } // Build the response from any resulting statuses