From 638899b103f65c1e0d6cd07e363848405c2da5d3 Mon Sep 17 00:00:00 2001 From: Gregor Gaertner Date: Mon, 18 Jan 2021 13:00:15 +0100 Subject: [PATCH] Improved exception handling in Calendar.java --- .../java/com/eternitywall/ots/Calendar.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/eternitywall/ots/Calendar.java b/src/main/java/com/eternitywall/ots/Calendar.java index e700593..5e114e3 100644 --- a/src/main/java/com/eternitywall/ots/Calendar.java +++ b/src/main/java/com/eternitywall/ots/Calendar.java @@ -91,7 +91,11 @@ public Timestamp submit(byte[] digest) throws ExceededSizeException, UrlExceptio StreamDeserializationContext ctx = new StreamDeserializationContext(body); return Timestamp.deserialize(ctx, digest); - } catch (Exception e) { + } catch (ExceededSizeException e) + { + throw e; + } + catch (Exception e) { throw new UrlException(e.getMessage()); } } @@ -101,11 +105,12 @@ public Timestamp submit(byte[] digest) throws ExceededSizeException, UrlExceptio * * @param commitment The digest hash to send. * @return the Timestamp from the calendar server (with blockchain information if already written). - * @throws DeserializationException if response is too big. + * @throws ExceededSizeException if response is too big. * @throws UrlException if url is not reachable. * @throws CommitmentNotFoundException if commit is not found. + * @throws DeserializationException if the data is corrupt */ - public Timestamp getTimestamp(byte[] commitment) throws DeserializationException, CommitmentNotFoundException, UrlException { + public Timestamp getTimestamp(byte[] commitment) throws DeserializationException, ExceededSizeException, CommitmentNotFoundException, UrlException { try { Map headers = new HashMap<>(); headers.put("Accept", "application/vnd.opentimestamps.v1"); @@ -118,7 +123,7 @@ public Timestamp getTimestamp(byte[] commitment) throws DeserializationException Response response = task.call(); byte[] body = response.getBytes(); if (body.length > 10000) { - throw new DeserializationException("Calendar response exceeded size limit"); + throw new ExceededSizeException("Calendar response exceeded size limit"); } if (!response.isOk()) { @@ -128,7 +133,12 @@ public Timestamp getTimestamp(byte[] commitment) throws DeserializationException StreamDeserializationContext ctx = new StreamDeserializationContext(body); return Timestamp.deserialize(ctx, commitment); - } catch (Exception e) { + } + catch (DeserializationException | ExceededSizeException | CommitmentNotFoundException e) + { + throw e; + } + catch (Exception e) { throw new UrlException(e.getMessage()); } }